Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class BrowserBackend extends EventEmitter<{ disconnected: [], dynamictool
responseObject = await response.serialize();
this._sessionLog?.logResponse(name, parsedArguments, responseObject);
} catch (error: any) {
const messages = [String(error), ...context.drainPendingUnhandledRejections().map(formatRejectionReason)];
const messages = [String(error), ...context.drainPendingUnhandledRejections().filter(reason => reason !== error).map(formatRejectionReason)];
responseObject = formatError(messages.join('\n\n'));
} finally {
context.setRunningTool(undefined);
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/tools/backend/runCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const runCode = defineTabTool({
// surfacing on the response.
const unsubscribe = tab.context.onUnhandledRejection(reason => {
if (!__end__.isDone())
__end__.reject(reason instanceof Error ? reason : new Error(String(reason)));
__end__.reject(reason as Error);
});
try {
await tab.waitForCompletion(async () => {
Expand Down
16 changes: 16 additions & 0 deletions tests/mcp/cli-run-code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,19 @@ test('run-code delayed route with setTimeout', async ({ cli, server }) => {
const { output } = await cli('eval', 'async () => { const r = await fetch("/api/slow"); return r.text(); }');
expect(output).toContain('loaded');
});

test('run-code reports route handler exception', async ({ cli, server }) => {
server.setContent('/', '<div>Hello</div>', 'text/html');
await cli('open', server.PREFIX);
const { output } = await cli('run-code', `async page => {
await page.route('**/route-throws', async route => {
throw new Error('route handler failed');
});
await page.goto('${server.PREFIX}/route-throws', { timeout: 5000 });
}`);
expect(output).toBe('### Error\nError: route handler failed');

server.setContent('/next', '<title>Next</title>', 'text/html');
const { output: gotoOutput } = await cli('goto', server.PREFIX + '/next');
expect(gotoOutput).toContain('Page Title: Next');
});