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
18 changes: 13 additions & 5 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,22 @@ 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');

// pre process the LHR for proto
const preprocessedLhr = preprocessor.processForProto(runnerResult.lhr);

// When LR is called with |internal: {keep_raw_response: true, save_lighthouse_assets: true}|,
// this code will log artifacts to raw_response.artifacts.
// we log artifacts to raw_response.artifacts.
if (logAssets) {
// @ts-ignore - piggyback the artifacts on the LHR.
runnerResult.lhr.artifacts = runnerResult.artifacts;
// Properly serialize artifact errors.
const artifactsJson = JSON.stringify(runnerResult.artifacts, assetSaver.stringifyReplacer);

return JSON.stringify({
...preprocessedLhr,
artifacts: JSON.parse(artifactsJson),
});
}

// pre process the LHR for proto
return JSON.stringify(preprocessor.processForProto(runnerResult.lhr));
return JSON.stringify(preprocessedLhr);
} catch (err) {
// If an error ruined the entire lighthouse run, attempt to return a meaningful error.
let runtimeError;
Expand Down
27 changes: 27 additions & 0 deletions clients/test/lightrider-entry-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,32 @@ describe('lightrider-entry', () => {

runStub.mockRestore();
});

it('exposes artifacts when logAssets is true', async () => {
const originalRun = Runner.run;
Runner.run = jest.fn().mockReturnValue(Promise.resolve({
lhr: {},
artifacts: {
Artifact: new Error('some error'),
},
}));
afterEach(() => {
Runner.run = originalRun;
});

const mockConnection = {};
const url = 'https://example.com';
const lrFlags = {
logAssets: true,
};
const resultJson = await lhBackground.runLighthouseInLR(mockConnection, url, {}, lrFlags);
const result = JSON.parse(resultJson);
expect(result.artifacts).toMatchObject({
Artifact: {
sentinel: '__ErrorSentinel',
message: 'some error',
},
});
});
});
});
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,
};