Skip to content

Commit b6b42ec

Browse files
fix: pageerror for non-error types (#442)
Closes #432
1 parent 5c871c3 commit b6b42ec

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/McpContext.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ export class McpContext implements Context {
103103
collect(event);
104104
},
105105
pageerror: event => {
106-
collect(event);
106+
if (event instanceof Error) {
107+
collect(event);
108+
} else {
109+
const error = new Error(`${event}`);
110+
error.stack = undefined;
111+
collect(error);
112+
}
107113
},
108114
} as ListenerMap;
109115
});

tests/tools/console.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ describe('console', () => {
3535
);
3636
});
3737
});
38+
39+
it('work with primitive unhandled errors', async () => {
40+
await withBrowser(async (response, context) => {
41+
const page = await context.newPage();
42+
await page.setContent('<script>throw undefined;</script>');
43+
await listConsoleMessages.handler({params: {}}, response, context);
44+
const formattedResponse = await response.handle('test', context);
45+
const textContent = formattedResponse[0] as {text: string};
46+
assert.ok(
47+
textContent.text.includes('msgid=1 [error] undefined (0 args)'),
48+
);
49+
});
50+
});
3851
});
3952

4053
describe('get_console_message', () => {

0 commit comments

Comments
 (0)