[go: nahoru, domu]

Skip to content

Commit

Permalink
clients(lightrider): serialize errors in artifacts (#9410)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored and patrickhulce committed Jul 24, 2019
1 parent fc3dc2e commit 82835eb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
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,
};

0 comments on commit 82835eb

Please sign in to comment.