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: add top-level runtimeError #6014

Merged
merged 7 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions lighthouse-core/lib/lh-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,6 @@ const ERRORS = {

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

19 changes: 14 additions & 5 deletions lighthouse-core/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,28 @@ class Runner {
}

/**
* Returns any runtimeError message found in artifacts.
* Returns first runtimeError found in artifacts.
* @param {LH.Artifacts} artifacts
* @return {string|undefined}
* @return {LH.Result['runtimeError']}
*/
static getArtifactRuntimeError(artifacts) {
for (const possibleErrorArtifact of Object.values(artifacts)) {
if (possibleErrorArtifact instanceof LHError && possibleErrorArtifact.lhrRuntimeError) {
const errorMessage = possibleErrorArtifact.friendlyMessage ?
`${possibleErrorArtifact.friendlyMessage} (${possibleErrorArtifact.message})` :
possibleErrorArtifact.message;
return errorMessage;
`${possibleErrorArtifact.friendlyMessage} (${possibleErrorArtifact.message})` :
possibleErrorArtifact.message;

return {
code: possibleErrorArtifact.code,
message: errorMessage,
};
}
}

return {
code: LHError.NO_ERROR,
message: '',
};
}

/**
Expand Down
28 changes: 22 additions & 6 deletions lighthouse-extension/app/src/lighthouse-ext-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const background = require('./lighthouse-background');
const ExtensionProtocol = require('../../../lighthouse-core/gather/connections/extension');
const log = require('lighthouse-logger');
const assetSaver = require('../../../lighthouse-core/lib/asset-saver.js');
const LHError = require('../../../lighthouse-core/lib/lh-error.js');

/** @type {Record<'mobile'|'desktop', LH.Config.Json>} */
const LR_PRESETS = {
Expand Down Expand Up @@ -111,14 +112,29 @@ async function runLighthouseInLR(connection, url, flags, {lrDevice, categoryIDs,
config.settings.onlyCategories = categoryIDs;
}

const results = await lighthouse(url, flags, config, connection);
if (!results) return;
try {
const results = await lighthouse(url, flags, config, connection);
if (!results) return;

if (logAssets) {
await assetSaver.logAssets(results.artifacts, results.lhr.audits);
}
if (logAssets) {
await assetSaver.logAssets(results.artifacts, results.lhr.audits);
}
return results.report;
} catch (err) {
// If an error ruined the entire lighthouse run, attempt to return a meaningful error.
if (!(err instanceof LHError) || !err.lhrRuntimeError) {
throw err;
Copy link
Member

Choose a reason for hiding this comment

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

In this situation I think we should not throw but instead return an "UNKNOWN_ERROR" i guess.

tbh i dont know what throwing does in this context. my guess is its not caught and triggers a timeout?

Copy link
Member Author

Choose a reason for hiding this comment

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

tbh i dont know what throwing does in this context. my guess is its not caught and triggers a timeout?

well it would throw and be up to the caller to deal with, but we can do UNKNOWN_ERROR instead :)

Copy link
Member Author

Choose a reason for hiding this comment

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

done

}

const runtimeError = {
code: err.code,
message: err.friendlyMessage ?
`${err.friendlyMessage} (${err.message})` :
err.message,
};

return results.report;
return JSON.stringify(runtimeError, null, 2);
Copy link
Member

Choose a reason for hiding this comment

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

I was thinking we're returning a psuedo LHR so one more level on top of this:

return JSON.stringify({runtimeError}, null, 2);

Copy link
Member Author

@brendankenny brendankenny Sep 14, 2018

Choose a reason for hiding this comment

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

yeah I missed that.

}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions typings/lhr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

import LHError = require('../lighthouse-core/lib/lh-error.js');

declare global {
module LH {
export type I18NMessageEntry = string | {path: string, values: any};
Expand Down Expand Up @@ -50,8 +52,8 @@ declare global {
configSettings: Config.Settings;
/** List of top-level warnings for this Lighthouse run. */
runWarnings: string[];
/** A top-level error message that, if present, indicates a serious enough problem that the result may need to be discarded. */
runtimeError: string|undefined;
/** A top-level error message that, if present, indicates a serious enough problem that this Lighthouse result may need to be discarded. */
runtimeError: {code: string, message: string};
/** The User-Agent string of the browser used run Lighthouse for these results. */
userAgent: string;
/** Information about the environment in which Lighthouse was run. */
Expand Down