Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: create flag to prevent fatal error on bad status code #15494

Merged
merged 13 commits into from
Oct 20, 2023
6 changes: 5 additions & 1 deletion cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,16 @@ function getYargsParser(manualArgv) {
type: 'boolean',
describe: 'Disables collection of the full page screenshot, which can be quite large',
},
'ignore-status-code': {
type: 'boolean',
describe: 'Disables failing on all error status codes, and instead issues a warning.',
},
})
.group([
'save-assets', 'list-all-audits', 'list-locales', 'list-trace-categories', 'additional-trace-categories',
'config-path', 'preset', 'chrome-flags', 'port', 'hostname', 'form-factor', 'screenEmulation', 'emulatedUserAgent',
'max-wait-for-load', 'enable-error-reporting', 'gather-mode', 'audit-mode',
'only-audits', 'only-categories', 'skip-audits', 'budget-path', 'disable-full-page-screenshot',
'only-audits', 'only-categories', 'skip-audits', 'budget-path', 'disable-full-page-screenshot', 'ignore-status-code',
], 'Configuration:')

// Output
Expand Down
1 change: 1 addition & 0 deletions core/config/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const defaultSettings = {
disableFullPageScreenshot: false,
skipAboutBlank: false,
blankPage: 'about:blank',
ignoreStatusCode: false,

// the following settings have no defaults but we still want ensure that `key in settings`
// in config will work in a typechecked way
Expand Down
1 change: 1 addition & 0 deletions core/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ async function _computeNavigationResult(
? getPageLoadError(navigationError, {
url: mainDocumentUrl,
loadFailureMode: navigationContext.navigation.loadFailureMode,
ignoreStatusCode: navigationContext.resolvedConfig.settings.ignoreStatusCode,
networkRecords: debugData.records,
warnings,
})
Expand Down
24 changes: 18 additions & 6 deletions core/lib/navigation-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
warningXhtml:
'The page MIME type is XHTML: Lighthouse does not explicitly support this document type',
/**
* Warning shown in report when the page under test returns an error code, which Lighthouse is not able to reliably load
* so we display a warning.
*/
warningStatusCode: 'Lighthouse was unable to reliably load the page you requested. Make sure' +
' you are testing the correct URL and that the server is properly responding' +
' to all requests.',
adrianaixba marked this conversation as resolved.
Show resolved Hide resolved
};

const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings);
Expand All @@ -27,9 +34,10 @@
/**
* Returns an error if the original network request failed or wasn't found.
* @param {LH.Artifacts.NetworkRequest|undefined} mainRecord
* @param {{warnings: Array<string | LH.IcuMessage>, ignoreStatusCode?: LH.Config.Settings['ignoreStatusCode']}} context
* @return {LH.LighthouseError|undefined}
*/
function getNetworkError(mainRecord) {
function getNetworkError(mainRecord, context) {
if (!mainRecord) {
return new LighthouseError(LighthouseError.errors.NO_DOCUMENT_REQUEST);
} else if (mainRecord.failed) {
Expand All @@ -47,9 +55,13 @@
LighthouseError.errors.FAILED_DOCUMENT_REQUEST, {errorDetails: netErr});
}
} else if (mainRecord.hasErrorStatusCode()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should generalize this to all error status codes (4xx/5xx). So I think it's better to use this mainRecord.hasErrorStatusCode() block instead of doing a === 404 check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does generalizing this to include all error status codes change our initial thoughts on the defaults? or do they feel good still?

  • devtools - ignore status code
  • node - fail on status code
  • cli - fail on status code
  • etc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the defaults should be the same as we discussed

return new LighthouseError(LighthouseError.errors.ERRORED_DOCUMENT_REQUEST, {
statusCode: `${mainRecord.statusCode}`,
});
if ( context.ignoreStatusCode) {
context.warnings.push(str_(UIStrings.warningStatusCode));

Check warning on line 59 in core/lib/navigation-error.js

View check run for this annotation

Codecov / codecov/patch

core/lib/navigation-error.js#L59

Added line #L59 was not covered by tests
} else {
return new LighthouseError(LighthouseError.errors.ERRORED_DOCUMENT_REQUEST, {
statusCode: `${mainRecord.statusCode}`,
});
}
}
}

Expand Down Expand Up @@ -113,7 +125,7 @@
* Returns an error if the page load should be considered failed, e.g. from a
* main document request failure, a security issue, etc.
* @param {LH.LighthouseError|undefined} navigationError
* @param {{url: string, loadFailureMode: LH.Config.SharedPassNavigationJson['loadFailureMode'], networkRecords: Array<LH.Artifacts.NetworkRequest>, warnings: Array<string | LH.IcuMessage>}} context
* @param {{url: string, loadFailureMode: LH.Config.SharedPassNavigationJson['loadFailureMode'], ignoreStatusCode?: LH.Config.Settings['ignoreStatusCode'], networkRecords: Array<LH.Artifacts.NetworkRequest>, warnings: Array<string | LH.IcuMessage>}} context
* @return {LH.LighthouseError|undefined}
*/
function getPageLoadError(navigationError, context) {
Expand Down Expand Up @@ -144,7 +156,7 @@
context.warnings.push(str_(UIStrings.warningXhtml));
}

const networkError = getNetworkError(mainRecord);
const networkError = getNetworkError(mainRecord, context);
const interstitialError = getInterstitialError(mainRecord, networkRecords);
const nonHtmlError = getNonHtmlError(finalRecord);

Expand Down
2 changes: 2 additions & 0 deletions core/scripts/i18n/collect-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ function checkKnownFixedCollisions(strings) {
'Document has a valid $MARKDOWN_SNIPPET_0$',
'Failing Elements',
'Failing Elements',
'Lighthouse was unable to reliably load the page you requested. Make sure you are testing the correct URL and that the server is properly responding to all requests.',
'Lighthouse was unable to reliably load the page you requested. Make sure you are testing the correct URL and that the server is properly responding to all requests.',
'Name',
'Name',
'Pages that use portals are not currently eligible for back/forward cache.',
Expand Down
Loading
Loading