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(tsc): don't use index signature in LHErrors #5896

Merged
merged 1 commit into from
Aug 22, 2018
Merged
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
64 changes: 38 additions & 26 deletions lighthouse-core/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const strings = require('./strings');

class LighthouseError extends Error {
/**
* @param {!LighthouseErrorDefinition} errorDefinition
* @param {!Object=} properties
* @param {LighthouseErrorDefinition} errorDefinition
* @param {Record<string, string|undefined>=} properties
*/
constructor(errorDefinition, properties) {
super(errorDefinition.code);
Expand All @@ -30,7 +30,7 @@ class LighthouseError extends Error {
}

/**
* @param {Error} err
* @param {{code?: string}} err
*/
static isPageLoadError(err) {
return err.code === ERRORS.NO_DOCUMENT_REQUEST.code ||
Expand All @@ -40,11 +40,11 @@ class LighthouseError extends Error {
/**
* @param {string} method
* @param {{message: string, data?: string|undefined}} protocolError
* @return {!Error|LighthouseError}
* @return {Error|LighthouseError}
*/
static fromProtocolMessage(method, protocolError) {
// extract all errors with a regex pattern to match against.
const protocolErrors = Object.keys(ERRORS).filter(k => ERRORS[k].pattern).map(k => ERRORS[k]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

so glad we can use Object.values now :)

const protocolErrors = Object.values(LighthouseError.errors).filter(e => e.pattern);
// if we find one, use the friendly LighthouseError definition
const matchedErrorDefinition = protocolErrors.find(e => e.pattern.test(protocolError.message));
if (matchedErrorDefinition) {
Expand All @@ -64,39 +64,51 @@ class LighthouseError extends Error {

const ERRORS = {
// Screenshot/speedline errors
NO_SPEEDLINE_FRAMES: {message: strings.didntCollectScreenshots},
SPEEDINDEX_OF_ZERO: {message: strings.didntCollectScreenshots},
NO_SCREENSHOTS: {message: strings.didntCollectScreenshots},
INVALID_SPEEDLINE: {message: strings.didntCollectScreenshots},
NO_SPEEDLINE_FRAMES: {code: 'NO_SPEEDLINE_FRAMES', message: strings.didntCollectScreenshots},
SPEEDINDEX_OF_ZERO: {code: 'SPEEDINDEX_OF_ZERO', message: strings.didntCollectScreenshots},
NO_SCREENSHOTS: {code: 'NO_SCREENSHOTS', message: strings.didntCollectScreenshots},
INVALID_SPEEDLINE: {code: 'INVALID_SPEEDLINE', message: strings.didntCollectScreenshots},

// Trace parsing errors
NO_TRACING_STARTED: {message: strings.badTraceRecording},
NO_NAVSTART: {message: strings.badTraceRecording},
NO_FCP: {message: strings.badTraceRecording},
NO_FMP: {message: strings.badTraceRecording},
NO_DCL: {message: strings.badTraceRecording},
NO_TRACING_STARTED: {code: 'NO_TRACING_STARTED', message: strings.badTraceRecording},
NO_NAVSTART: {code: 'NO_NAVSTART', message: strings.badTraceRecording},
NO_FCP: {code: 'NO_FCP', message: strings.badTraceRecording},
NO_FMP: {code: 'NO_FMP', message: strings.badTraceRecording},
NO_DCL: {code: 'NO_DCL', message: strings.badTraceRecording},

// TTI calculation failures
FMP_TOO_LATE_FOR_FCPUI: {message: strings.pageLoadTookTooLong},
NO_FCPUI_IDLE_PERIOD: {message: strings.pageLoadTookTooLong},
NO_TTI_CPU_IDLE_PERIOD: {message: strings.pageLoadTookTooLong},
NO_TTI_NETWORK_IDLE_PERIOD: {message: strings.pageLoadTookTooLong},
FMP_TOO_LATE_FOR_FCPUI: {code: 'FMP_TOO_LATE_FOR_FCPUI', message: strings.pageLoadTookTooLong},
NO_FCPUI_IDLE_PERIOD: {code: 'NO_FCPUI_IDLE_PERIOD', message: strings.pageLoadTookTooLong},
NO_TTI_CPU_IDLE_PERIOD: {code: 'NO_TTI_CPU_IDLE_PERIOD', message: strings.pageLoadTookTooLong},
NO_TTI_NETWORK_IDLE_PERIOD: {
code: 'NO_TTI_NETWORK_IDLE_PERIOD',
message: strings.pageLoadTookTooLong,
},

// Page load failures
NO_DOCUMENT_REQUEST: {message: strings.pageLoadFailed},
FAILED_DOCUMENT_REQUEST: {message: strings.pageLoadFailed},
NO_DOCUMENT_REQUEST: {code: 'NO_DOCUMENT_REQUEST', message: strings.pageLoadFailed},
FAILED_DOCUMENT_REQUEST: {code: 'FAILED_DOCUMENT_REQUEST', message: strings.pageLoadFailed},

// Protocol internal failures
TRACING_ALREADY_STARTED: {message: strings.internalChromeError, pattern: /Tracing.*started/},
PARSING_PROBLEM: {message: strings.internalChromeError, pattern: /Parsing problem/},
READ_FAILED: {message: strings.internalChromeError, pattern: /Read failed/},
TRACING_ALREADY_STARTED: {
code: 'TRACING_ALREADY_STARTED',
message: strings.internalChromeError,
pattern: /Tracing.*started/,
},
PARSING_PROBLEM: {
code: 'PARSING_PROBLEM',
message: strings.internalChromeError,
pattern: /Parsing problem/,
},
READ_FAILED: {code: 'READ_FAILED', message: strings.internalChromeError, pattern: /Read failed/},

// Protocol timeout failures
REQUEST_CONTENT_TIMEOUT: {message: strings.requestContentTimeout},
REQUEST_CONTENT_TIMEOUT: {
code: 'REQUEST_CONTENT_TIMEOUT',
message: strings.requestContentTimeout,
},
};

Object.keys(ERRORS).forEach(code => ERRORS[code].code = code);

/** @type {Record<keyof typeof ERRORS, LighthouseErrorDefinition>} */
LighthouseError.errors = ERRORS;
module.exports = LighthouseError;
Expand Down