fix: fix line/column number in errors (#10926)

This commit is contained in:
jrandolf 2023-09-18 11:52:04 +02:00 committed by GitHub
parent a4345a477f
commit a0e57f7eb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 19 deletions

View File

@ -344,24 +344,27 @@ export class BidiPage extends Page {
) )
); );
} else if (isJavaScriptLogEntry(event)) { } else if (isJavaScriptLogEntry(event)) {
let message = event.text ?? ''; const error = new Error(event.text ?? '');
const messageHeight = error.message.split('\n').length;
const messageLines = error.stack!.split('\n').splice(0, messageHeight);
const stackLines = [];
if (event.stackTrace) { if (event.stackTrace) {
for (const callFrame of event.stackTrace.callFrames) { for (const frame of event.stackTrace.callFrames) {
const location = // Note we need to add `1` because the values are 0-indexed.
callFrame.url + stackLines.push(
':' + ` at ${frame.functionName || '<anonymous>'} (${frame.url}:${
callFrame.lineNumber + frame.lineNumber + 1
':' + }:${frame.columnNumber + 1})`
callFrame.columnNumber; );
const functionName = callFrame.functionName || '<anonymous>'; if (stackLines.length >= Error.stackTraceLimit) {
message += `\n at ${functionName} (${location})`; break;
}
} }
} }
const error = new Error(message); error.stack = [...messageLines, ...stackLines].join('\n');
error.stack = ''; // Don't capture Puppeteer stacktrace.
this.emit(PageEvent.PageError, error); this.emit(PageEvent.PageError, error);
} else { } else {
debugError( debugError(

View File

@ -124,18 +124,20 @@ export function createClientError(
name = detail.name; name = detail.name;
message = detail.message; message = detail.message;
} }
const messageHeight = message.split('\n').length;
const error = new Error(message); const error = new Error(message);
error.name = name; error.name = name;
const stackLines = []; const messageHeight = error.message.split('\n').length;
const messageLines = error.stack!.split('\n').splice(0, messageHeight); const messageLines = error.stack!.split('\n').splice(0, messageHeight);
if (details.stackTrace && stackLines.length < Error.stackTraceLimit) {
for (const frame of details.stackTrace.callFrames.reverse()) { const stackLines = [];
if (details.stackTrace) {
for (const frame of details.stackTrace.callFrames) {
// Note we need to add `1` because the values are 0-indexed.
stackLines.push( stackLines.push(
` at ${frame.functionName || '<anonymous>'} (${frame.url}:${ ` at ${frame.functionName || '<anonymous>'} (${frame.url}:${
frame.lineNumber frame.lineNumber + 1
}:${frame.columnNumber})` }:${frame.columnNumber + 1})`
); );
if (stackLines.length >= Error.stackTraceLimit) { if (stackLines.length >= Error.stackTraceLimit) {
break; break;

View File

@ -1369,6 +1369,7 @@ describe('Page', function () {
page.goto(server.PREFIX + '/error.html'), page.goto(server.PREFIX + '/error.html'),
]); ]);
expect(error.message).toContain('Fancy'); expect(error.message).toContain('Fancy');
expect(error.stack?.split('\n')[1]).toContain('error.html:13');
}); });
}); });