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(lantern): add LanternError and adapter to LH error #15937

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions core/computed/metrics/lantern-first-contentful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import {makeComputedArtifact} from '../computed-artifact.js';
import {getComputationDataParams} from './lantern-metric.js';
import {getComputationDataParams, lanternErrorAdapter} from './lantern-metric.js';
import {FirstContentfulPaint} from '../../lib/lantern/metrics/first-contentful-paint.js';

/** @typedef {import('../../lib/lantern/metric.js').Extras} Extras */
Expand All @@ -18,7 +18,8 @@ class LanternFirstContentfulPaint extends FirstContentfulPaint {
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static async computeMetricWithGraphs(data, context, extras) {
return this.compute(await getComputationDataParams(data, context), extras);
return this.compute(await getComputationDataParams(data, context), extras)
.catch(lanternErrorAdapter);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions core/computed/metrics/lantern-first-meaningful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import {makeComputedArtifact} from '../computed-artifact.js';
import {getComputationDataParams} from './lantern-metric.js';
import {getComputationDataParams, lanternErrorAdapter} from './lantern-metric.js';
import {FirstMeaningfulPaint} from '../../lib/lantern/metrics/first-meaningful-paint.js';
import {LanternFirstContentfulPaint} from './lantern-first-contentful-paint.js';

Expand All @@ -19,7 +19,8 @@ class LanternFirstMeaningfulPaint extends FirstMeaningfulPaint {
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static async computeMetricWithGraphs(data, context, extras) {
return this.compute(await getComputationDataParams(data, context), extras);
return this.compute(await getComputationDataParams(data, context), extras)
.catch(lanternErrorAdapter);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions core/computed/metrics/lantern-interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {makeComputedArtifact} from '../computed-artifact.js';
import {LanternFirstMeaningfulPaint} from './lantern-first-meaningful-paint.js';
import {Interactive} from '../../lib/lantern/metrics/interactive.js';
import {getComputationDataParams} from './lantern-metric.js';
import {getComputationDataParams, lanternErrorAdapter} from './lantern-metric.js';

/** @typedef {import('../../lib/lantern/metric.js').Extras} Extras */

Expand All @@ -19,7 +19,8 @@ class LanternInteractive extends Interactive {
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static async computeMetricWithGraphs(data, context, extras) {
return this.compute(await getComputationDataParams(data, context), extras);
return this.compute(await getComputationDataParams(data, context), extras)
.catch(lanternErrorAdapter);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions core/computed/metrics/lantern-largest-contentful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import {makeComputedArtifact} from '../computed-artifact.js';
import {LargestContentfulPaint} from '../../lib/lantern/metrics/largest-contentful-paint.js';
import {getComputationDataParams} from './lantern-metric.js';
import {getComputationDataParams, lanternErrorAdapter} from './lantern-metric.js';
import {LanternFirstContentfulPaint} from './lantern-first-contentful-paint.js';

/** @typedef {import('../../lib/lantern/metric.js').Extras} Extras */
Expand All @@ -19,7 +19,8 @@ class LanternLargestContentfulPaint extends LargestContentfulPaint {
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static async computeMetricWithGraphs(data, context, extras) {
return this.compute(await getComputationDataParams(data, context), extras);
return this.compute(await getComputationDataParams(data, context), extras)
.catch(lanternErrorAdapter);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions core/computed/metrics/lantern-max-potential-fid.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import {makeComputedArtifact} from '../computed-artifact.js';
import {MaxPotentialFID} from '../../lib/lantern/metrics/max-potential-fid.js';
import {getComputationDataParams} from './lantern-metric.js';
import {getComputationDataParams, lanternErrorAdapter} from './lantern-metric.js';
import {LanternFirstContentfulPaint} from './lantern-first-contentful-paint.js';

/** @typedef {import('../../lib/lantern/metric.js').Extras} Extras */
Expand All @@ -19,7 +19,8 @@ class LanternMaxPotentialFID extends MaxPotentialFID {
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static async computeMetricWithGraphs(data, context, extras) {
return this.compute(await getComputationDataParams(data, context), extras);
return this.compute(await getComputationDataParams(data, context), extras)
.catch(lanternErrorAdapter);
}

/**
Expand Down
24 changes: 23 additions & 1 deletion core/computed/metrics/lantern-metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {LanternError} from '../../lib/lantern/lantern-error.js';
import {LighthouseError} from '../../lib/lh-error.js';
import {LoadSimulator} from '../load-simulator.js';
import {PageDependencyGraph} from '../page-dependency-graph.js';
import {ProcessedNavigation} from '../processed-navigation.js';
Expand All @@ -24,4 +26,24 @@ async function getComputationDataParams(data, context) {
return {simulator, graph, processedNavigation};
}

export {getComputationDataParams};
/**
* @param {unknown} err
* @return {never}
*/
function lanternErrorAdapter(err) {
if (!(err instanceof LanternError)) {
throw err;
}

const code = /** @type {keyof LighthouseError.errors} */ (err.message);
if (LighthouseError.errors[code]) {
throw new LighthouseError(LighthouseError.errors[code]);
}

throw err;
}

export {
getComputationDataParams,
lanternErrorAdapter,
};
5 changes: 3 additions & 2 deletions core/computed/metrics/lantern-speed-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import {makeComputedArtifact} from '../computed-artifact.js';
import {getComputationDataParams} from './lantern-metric.js';
import {getComputationDataParams, lanternErrorAdapter} from './lantern-metric.js';
import {Speedline} from '../speedline.js';
import {LanternFirstContentfulPaint} from './lantern-first-contentful-paint.js';
import {SpeedIndex} from '../../lib/lantern/metrics/speed-index.js';
Expand All @@ -20,7 +20,8 @@ class LanternSpeedIndex extends SpeedIndex {
* @return {Promise<LH.Artifacts.LanternMetric>}
*/
static async computeMetricWithGraphs(data, context, extras) {
return this.compute(await getComputationDataParams(data, context), extras);
return this.compute(await getComputationDataParams(data, context), extras)
.catch(lanternErrorAdapter);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions core/lib/lantern/lantern-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

class LanternError extends Error {}

export {LanternError};
6 changes: 2 additions & 4 deletions core/lib/lantern/metrics/first-meaningful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import * as Lantern from '../types/lantern.js';
import {Metric} from '../metric.js';
// TODO(15841): don't use LighthouseError
import {LighthouseError} from '../../lh-error.js';
import {FirstContentfulPaint} from './first-contentful-paint.js';

/** @typedef {import('../base-node.js').Node} Node */
Expand All @@ -32,7 +30,7 @@ class FirstMeaningfulPaint extends Metric {
static getOptimisticGraph(dependencyGraph, processedNavigation) {
const fmp = processedNavigation.timestamps.firstMeaningfulPaint;
if (!fmp) {
throw new LighthouseError(LighthouseError.errors.NO_FMP);
throw new Error('NO_FMP');
}
return FirstContentfulPaint.getFirstPaintBasedGraph(dependencyGraph, {
cutoffTimestamp: fmp,
Expand All @@ -51,7 +49,7 @@ class FirstMeaningfulPaint extends Metric {
static getPessimisticGraph(dependencyGraph, processedNavigation) {
const fmp = processedNavigation.timestamps.firstMeaningfulPaint;
if (!fmp) {
throw new LighthouseError(LighthouseError.errors.NO_FMP);
throw new Error('NO_FMP');
}

return FirstContentfulPaint.getFirstPaintBasedGraph(dependencyGraph, {
Expand Down
6 changes: 3 additions & 3 deletions core/lib/lantern/metrics/largest-contentful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import * as Lantern from '../types/lantern.js';
import {Metric} from '../metric.js';
import {LighthouseError} from '../../../lib/lh-error.js';
import {FirstContentfulPaint} from './first-contentful-paint.js';
import {LanternError} from '../lantern-error.js';

/** @typedef {import('../base-node.js').Node} Node */

Expand Down Expand Up @@ -45,7 +45,7 @@ class LargestContentfulPaint extends Metric {
static getOptimisticGraph(dependencyGraph, processedNavigation) {
const lcp = processedNavigation.timestamps.largestContentfulPaint;
if (!lcp) {
throw new LighthouseError(LighthouseError.errors.NO_LCP);
throw new LanternError('NO_LCP');
}

return FirstContentfulPaint.getFirstPaintBasedGraph(dependencyGraph, {
Expand All @@ -62,7 +62,7 @@ class LargestContentfulPaint extends Metric {
static getPessimisticGraph(dependencyGraph, processedNavigation) {
const lcp = processedNavigation.timestamps.largestContentfulPaint;
if (!lcp) {
throw new LighthouseError(LighthouseError.errors.NO_LCP);
throw new LanternError('NO_LCP');
}

return FirstContentfulPaint.getFirstPaintBasedGraph(dependencyGraph, {
Expand Down
23 changes: 14 additions & 9 deletions core/test/computed/metrics/largest-contentful-paint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ Object {
assert.equal(result.timestamp, 713038144775);
});

it('should fail to compute an observed value for old trace', async () => {
const settings = {throttlingMethod: 'provided'};
const context = {settings, computedCache: new Map()};
const URL = getURLArtifactFromDevtoolsLog(invalidDevtoolsLog);
const resultPromise = LargestContentfulPaint.request(
{gatherContext, trace: invalidTrace, devtoolsLog: invalidDevtoolsLog, settings, URL},
context
);
await expect(resultPromise).rejects.toThrow('NO_LCP');
['provided', 'simulate'].forEach(throttlingMethod => {
it(`should fail to compute a value for old trace (${throttlingMethod})`, async () => {
const settings = {throttlingMethod};
const context = {settings, computedCache: new Map()};
const URL = getURLArtifactFromDevtoolsLog(invalidDevtoolsLog);
const resultPromise = LargestContentfulPaint.request(
{gatherContext, trace: invalidTrace, devtoolsLog: invalidDevtoolsLog, settings, URL},
context
);
await expect(resultPromise).rejects.toMatchObject({
code: 'NO_LCP',
friendlyMessage: expect.toBeDisplayString(/The page did not display content.*NO_LCP/),
});
});
});
});
Loading