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

clients(lightrider): serialize errors in artifacts #9410

Merged
merged 13 commits into from
Jul 24, 2019
23 changes: 16 additions & 7 deletions clients/lightrider/lightrider-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const lighthouse = require('../../lighthouse-core/index.js');

const LHError = require('../../lighthouse-core/lib/lh-error.js');
const preprocessor = require('../../lighthouse-core/lib/proto-preprocessor.js');
const assetSaver = require('../../lighthouse-core/lib/asset-saver.js');

/** @type {Record<'mobile'|'desktop', LH.Config.Json>} */
const LR_PRESETS = {
Expand Down Expand Up @@ -54,15 +55,23 @@ async function runLighthouseInLR(connection, url, flags, lrOpts) {
const runnerResult = await lighthouse(url, flags, config, connection);
if (!runnerResult) throw new Error('Lighthouse finished without a runnerResult');

// When LR is called with |internal: {keep_raw_response: true, save_lighthouse_assets: true}|,
// this code will log artifacts to raw_response.artifacts.
// pre process the LHR for proto
const preprocessedLhr = preprocessor.processForProto(runnerResult.lhr);

if (logAssets) {
// @ts-ignore - piggyback the artifacts on the LHR.
runnerResult.lhr.artifacts = runnerResult.artifacts;
}
// When LR is called with |internal: {keep_raw_response: true, save_lighthouse_assets: true}|,
Copy link
Member

Choose a reason for hiding this comment

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

Move comment to be above if (logAssets) {

// this code will log artifacts to raw_response.artifacts.

// pre process the LHR for proto
return JSON.stringify(preprocessor.processForProto(runnerResult.lhr));
// Properly serialize artifact errors.
const artifactsJson = JSON.stringify(runnerResult.artifacts, assetSaver.stringifyReplacer);

return JSON.stringify({
...preprocessedLhr,
artifacts: JSON.parse(artifactsJson),
});
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Can't decide if the else is necessary or not. I'm flip flopping on keeping it in/removing it is more readable.

Copy link
Member

Choose a reason for hiding this comment

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

Can't decide if the else is necessary or not. I'm flip flopping on keeping it in/removing it is more readable.

I'd fall on the side of no else, but really up to @connorjclark, it's fine either way

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

can you be more specific about what you are suggesting instead

Copy link
Member

Choose a reason for hiding this comment

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

You're returning in both sides of the if/else. You can just remove the else here to always return JSON.stringify(preprocessedLhr).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

do you see what i'm doing with artifacts tho.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh i see now

return JSON.stringify(preprocessedLhr);
}
} catch (err) {
// If an error ruined the entire lighthouse run, attempt to return a meaningful error.
let runtimeError;
Expand Down
1 change: 1 addition & 0 deletions lighthouse-core/lib/asset-saver.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,5 @@ module.exports = {
prepareAssets,
saveTrace,
saveLanternNetworkData,
stringifyReplacer,
};