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(gather-runner): include error status codes in pageLoadError #6051

Merged
merged 4 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions lighthouse-cli/test/smokehouse/seo/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ module.exports = [
},
},
{
requestedUrl: BASE_URL + 'seo-failure-cases.html?status_code=403&' + failureHeaders,
finalUrl: BASE_URL + 'seo-failure-cases.html?status_code=403&' + failureHeaders,
requestedUrl: BASE_URL + 'seo-failure-cases.html?' + failureHeaders,
finalUrl: BASE_URL + 'seo-failure-cases.html?' + failureHeaders,
audits: {
'viewport': {
score: 0,
Expand All @@ -91,8 +91,7 @@ module.exports = [
score: 0,
},
'http-status-code': {
score: 0,
displayValue: '403',
score: 1,
},
'font-size': {
rawValue: false,
Expand Down Expand Up @@ -137,4 +136,42 @@ module.exports = [
},
},
},
{
// Note: most scores are null (audit error) because the page 403ed.
requestedUrl: BASE_URL + 'seo-failure-cases.html?status_code=403',
finalUrl: BASE_URL + 'seo-failure-cases.html?status_code=403',
audits: {
'http-status-code': {
score: 0,
displayValue: '403',
},
'viewport': {
score: null,
},
'document-title': {
score: null,
},
'meta-description': {
score: null,
},
'font-size': {
score: null,
},
'link-text': {
score: null,
},
'is-crawlable': {
score: null,
},
'hreflang': {
score: null,
},
'plugins': {
score: null,
},
'canonical': {
score: null,
},
},
},
];
3 changes: 3 additions & 0 deletions lighthouse-core/gather/gather-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ class GatherRunner {
} else if (mainRecord.failed) {
errorCode = LHError.errors.FAILED_DOCUMENT_REQUEST;
errorReason = mainRecord.localizedFailDescription;
} else if (mainRecord.hasErrorStatusCode()) {
errorCode = LHError.errors.ERRORED_DOCUMENT_REQUEST;
errorReason = `Status code: ${mainRecord.statusCode}`;
}

if (errorCode) {
Expand Down
7 changes: 7 additions & 0 deletions lighthouse-core/lib/lh-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,18 @@ const ERRORS = {
message: strings.pageLoadFailed,
lhrRuntimeError: true,
},
/* Used when DevTools reports loading failed. Usually an internal (Chrome) issue. */
FAILED_DOCUMENT_REQUEST: {
code: 'FAILED_DOCUMENT_REQUEST',
message: strings.pageLoadFailed,
lhrRuntimeError: true,
},
/* Used when status code is 4xx or 5xx. */
ERRORED_DOCUMENT_REQUEST: {
code: 'ERRORED_DOCUMENT_REQUEST',
message: strings.pageLoadFailed,
lhrRuntimeError: true,
},

// Protocol internal failures
TRACING_ALREADY_STARTED: {
Expand Down
7 changes: 7 additions & 0 deletions lighthouse-core/lib/network-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ module.exports = class NetworkRequest {
this.isLinkPreload = false;
}

/**
* @return {boolean}
*/
hasErrorStatusCode() {
return this.statusCode >= 400;
}

/**
* @param {NetworkRequest} initiator
*/
Expand Down
42 changes: 34 additions & 8 deletions lighthouse-core/test/gather/gather-runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const GatherRunner = require('../../gather/gather-runner');
const assert = require('assert');
const Config = require('../../config/config');
const unresolvedPerfLog = require('./../fixtures/unresolved-perflog.json');
const NetworkRequest = require('../../lib/network-request.js');

class TestGatherer extends Gatherer {
constructor() {
Expand Down Expand Up @@ -606,31 +607,56 @@ describe('GatherRunner', function() {
describe('#getPageLoadError', () => {
it('passes when the page is loaded', () => {
const url = 'http://the-page.com';
const records = [{url}];
assert.ok(!GatherRunner.getPageLoadError(url, records));
const mainRecord = new NetworkRequest();
mainRecord.url = url;
assert.ok(!GatherRunner.getPageLoadError(url, [mainRecord]));
});

it('passes when the page is loaded, ignoring any fragment', () => {
const url = 'http://example.com/#/page/list';
const records = [{url: 'http://example.com'}];
assert.ok(!GatherRunner.getPageLoadError(url, records));
const mainRecord = new NetworkRequest();
mainRecord.url = 'http://example.com';
assert.ok(!GatherRunner.getPageLoadError(url, [mainRecord]));
});

it('throws when page fails to load', () => {
it('fails when page fails to load', () => {
const url = 'http://the-page.com';
const records = [{url, failed: true, localizedFailDescription: 'foobar'}];
const error = GatherRunner.getPageLoadError(url, records);
const mainRecord = new NetworkRequest();
mainRecord.url = url;
mainRecord.failed = true;
mainRecord.localizedFailDescription = 'foobar';
const error = GatherRunner.getPageLoadError(url, [mainRecord]);
assert.equal(error.message, 'FAILED_DOCUMENT_REQUEST');
assert.ok(/Your page failed to load/.test(error.friendlyMessage));
});

it('throws when page times out', () => {
it('fails when page times out', () => {
const url = 'http://the-page.com';
const records = [];
const error = GatherRunner.getPageLoadError(url, records);
assert.equal(error.message, 'NO_DOCUMENT_REQUEST');
assert.ok(/Your page failed to load/.test(error.friendlyMessage));
});

it('fails when page returns with a 404', () => {
const url = 'http://the-page.com';
const mainRecord = new NetworkRequest();
mainRecord.url = url;
mainRecord.statusCode = 404;
const error = GatherRunner.getPageLoadError(url, [mainRecord]);
assert.equal(error.message, 'ERRORED_DOCUMENT_REQUEST');
assert.ok(/Your page failed to load/.test(error.friendlyMessage));
});

it('fails when page returns with a 500', () => {
const url = 'http://the-page.com';
const mainRecord = new NetworkRequest();
mainRecord.url = url;
mainRecord.statusCode = 500;
const error = GatherRunner.getPageLoadError(url, [mainRecord]);
assert.equal(error.message, 'ERRORED_DOCUMENT_REQUEST');
assert.ok(/Your page failed to load/.test(error.friendlyMessage));
});
});

describe('artifact collection', () => {
Expand Down