From 3ea15dc39512a4576861d29ae8b6c08ac1373b7e Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Tue, 21 Mar 2023 10:21:48 +0100 Subject: [PATCH] chore: JavaScript error should produce `pageerror` events (#9884) --- .../puppeteer-core/src/common/bidi/Page.ts | 35 ++++++++++++++----- test/TestExpectations.json | 6 ++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/packages/puppeteer-core/src/common/bidi/Page.ts b/packages/puppeteer-core/src/common/bidi/Page.ts index b5177da8266..c709eb7b41b 100644 --- a/packages/puppeteer-core/src/common/bidi/Page.ts +++ b/packages/puppeteer-core/src/common/bidi/Page.ts @@ -25,6 +25,7 @@ import { import {ConsoleMessage, ConsoleMessageLocation} from '../ConsoleMessage.js'; import {Handler} from '../EventEmitter.js'; import {EvaluateFunc, HandleFor} from '../types.js'; +import {debugError} from '../util.js'; import {Context, getBidiHandle} from './Context.js'; import {BidiSerializer} from './Serializer.js'; @@ -81,14 +82,28 @@ export class Page extends PageBase { ) ); } else if (isJavaScriptLogEntry(event)) { - this.emit( - PageEmittedEvents.Console, - new ConsoleMessage( - event.level as any, - event.text ?? '', - [], - getStackTraceLocations(event.stackTrace) - ) + let message = event.text ?? ''; + + if (event.stackTrace) { + for (const callFrame of event.stackTrace.callFrames) { + const location = + callFrame.url + + ':' + + callFrame.lineNumber + + ':' + + callFrame.columnNumber; + const functionName = callFrame.functionName || ''; + message += `\n at ${functionName} (${location})`; + } + } + + const error = new Error(message); + error.stack = ''; // Don't capture Puppeteer stacktrace. + + this.emit(PageEmittedEvents.PageError, error); + } else { + debugError( + `Unhandled LogEntry with type "${event.type}", text "${event.text}" and level "${event.level}"` ); } } @@ -191,7 +206,9 @@ function isJavaScriptLogEntry( return event.type === 'javascript'; } -function getStackTraceLocations(stackTrace?: Bidi.Script.StackTrace) { +function getStackTraceLocations( + stackTrace?: Bidi.Script.StackTrace +): ConsoleMessageLocation[] { const stackTraceLocations: ConsoleMessageLocation[] = []; if (stackTrace) { for (const callFrame of stackTrace.callFrames) { diff --git a/test/TestExpectations.json b/test/TestExpectations.json index db98e5889d5..cf188745e13 100644 --- a/test/TestExpectations.json +++ b/test/TestExpectations.json @@ -2050,5 +2050,11 @@ "platforms": ["darwin", "linux", "win32"], "parameters": ["chrome", "webDriverBiDi"], "expectations": ["FAIL"] + }, + { + "testIdPattern": "[page.spec] Page Page.Events.PageError *", + "platforms": ["darwin", "linux", "win32"], + "parameters": ["webDriverBiDi"], + "expectations": ["PASS"] } ]