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

fix: fix line/column number in errors #10926

Merged
merged 1 commit into from
Sep 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 16 additions & 13 deletions packages/puppeteer-core/src/bidi/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,24 +344,27 @@ export class BidiPage extends Page {
)
);
} 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) {
for (const callFrame of event.stackTrace.callFrames) {
const location =
callFrame.url +
':' +
callFrame.lineNumber +
':' +
callFrame.columnNumber;
const functionName = callFrame.functionName || '<anonymous>';
message += `\n at ${functionName} (${location})`;
for (const frame of event.stackTrace.callFrames) {
// Note we need to add `1` because the values are 0-indexed.
stackLines.push(
` at ${frame.functionName || '<anonymous>'} (${frame.url}:${
frame.lineNumber + 1
}:${frame.columnNumber + 1})`
);
if (stackLines.length >= Error.stackTraceLimit) {
break;
}
}
}

const error = new Error(message);
error.stack = ''; // Don't capture Puppeteer stacktrace.

error.stack = [...messageLines, ...stackLines].join('\n');
this.emit(PageEvent.PageError, error);
} else {
debugError(
Expand Down
14 changes: 8 additions & 6 deletions packages/puppeteer-core/src/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,20 @@ export function createClientError(
name = detail.name;
message = detail.message;
}
const messageHeight = message.split('\n').length;
const error = new Error(message);
error.name = name;

const stackLines = [];
const messageHeight = error.message.split('\n').length;
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(
` at ${frame.functionName || '<anonymous>'} (${frame.url}:${
frame.lineNumber
}:${frame.columnNumber})`
frame.lineNumber + 1
jrandolf marked this conversation as resolved.
Show resolved Hide resolved
}:${frame.columnNumber + 1})`
);
if (stackLines.length >= Error.stackTraceLimit) {
break;
Expand Down
1 change: 1 addition & 0 deletions test/src/page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,7 @@ describe('Page', function () {
page.goto(server.PREFIX + '/error.html'),
]);
expect(error.message).toContain('Fancy');
expect(error.stack?.split('\n')[1]).toContain('error.html:13');
});
});

Expand Down