Skip to content

Commit f79bce3

Browse files
fix: handle Lighthouse internal errors
1 parent 5380ea6 commit f79bce3

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

cli.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,43 @@ async function runLighthouse(url) {
233233
],
234234
});
235235

236-
const result = await lighthouse(url, {
237-
port: 9222,
238-
output: "html",
239-
logLevel: "error",
240-
});
236+
// Capture console output to filter Lighthouse internal errors
237+
const originalConsoleError = console.error;
238+
const lighthouseErrors = [];
239+
240+
console.error = (...args) => {
241+
const message = args.join(" ");
242+
if (
243+
message.includes("LanternError") ||
244+
message.includes("Invalid dependency graph")
245+
) {
246+
lighthouseErrors.push(message);
247+
// Show a cleaner warning instead of the full stack trace
248+
if (lighthouseErrors.length === 1) {
249+
console.warn(
250+
chalk.yellow(
251+
" └─ ⚠️ Lighthouse internal warning (analysis will continue)"
252+
)
253+
);
254+
}
255+
} else {
256+
originalConsoleError(...args);
257+
}
258+
};
259+
260+
try {
261+
const result = await lighthouse(url, {
262+
port: 9222,
263+
output: "html",
264+
logLevel: "error",
265+
});
241266

242-
await browser.close();
243-
return result;
267+
return result;
268+
} finally {
269+
// Restore original console.error
270+
console.error = originalConsoleError;
271+
await browser.close();
272+
}
244273
}
245274

246275
(async () => {
@@ -291,7 +320,7 @@ async function runLighthouse(url) {
291320
if (actualScore < options.threshold) {
292321
console.warn(
293322
chalk.red(
294-
`⚠️ Score ${actualScore} is below threshold of ${options.threshold}`
323+
`⚠️ Score ${actualScore} is below threshold of ${options.threshold}`
295324
)
296325
);
297326
process.exitCode = 1; // does not exit immediately, just sets failure
@@ -317,7 +346,7 @@ async function runLighthouse(url) {
317346

318347
if (urls.length > accessibleUrls.length) {
319348
console.log(
320-
`${chalk.yellow("⚠️ Skipped (inaccessible):")} ${
349+
`${chalk.yellow("⚠️ Skipped (inaccessible):")} ${
321350
urls.length - accessibleUrls.length
322351
}`
323352
);

0 commit comments

Comments
 (0)