Skip to content

Commit cb719d7

Browse files
committed
fix: improve error handling for console messages
1 parent 0610d11 commit cb719d7

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

src/McpResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export class McpResponse implements Response {
337337
const devTools = context.getDevToolsUniverse();
338338
return await ConsoleFormatter.from(consoleMessage, {
339339
id: consoleMessageStableId,
340-
fetchDetailedData: true,
340+
fetchDetailedData: false,
341341
devTools: devTools ?? undefined,
342342
});
343343
}

src/formatters/ConsoleFormatter.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,24 @@ export class ConsoleFormatter {
5050
}
5151

5252
this.#resolvedArgs = await Promise.all(
53-
this.#msg.args().map(arg => arg.jsonValue()),
53+
this.#msg.args().map(async (arg, i) => {
54+
try {
55+
return await arg.jsonValue();
56+
} catch {
57+
return `<error: Argument ${i} is no longer available>`;
58+
}
59+
}),
5460
);
5561

5662
if (devTools) {
57-
this.#resolvedStackTrace = await createStackTraceForConsoleMessage(
58-
devTools,
59-
this.#msg,
60-
);
63+
try {
64+
this.#resolvedStackTrace = await createStackTraceForConsoleMessage(
65+
devTools,
66+
this.#msg,
67+
);
68+
} catch (err) {
69+
// ignore
70+
}
6171
}
6272
}
6373

tests/formatters/ConsoleFormatter.test.js.snapshot

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ Message: log> Processing file:
4444
### Arguments
4545
Arg #0: file.txt
4646
`;
47+
48+
exports[`ConsoleFormatter > toStringDetailed > handles \"Execution context is not available\" error in args 1`] = `
49+
ID: 6
50+
Message: log> Processing file:
51+
### Arguments
52+
Arg #0: <error: Argument 0 is no longer available>
53+
`;

tests/formatters/ConsoleFormatter.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,27 @@ describe('ConsoleFormatter', () => {
163163
).toStringDetailed();
164164
t.assert.snapshot?.(result);
165165
});
166+
167+
it('handles "Execution context is not available" error in args', async t => {
168+
const message = createMockMessage({
169+
type: () => 'log',
170+
text: () => 'Processing file:',
171+
args: () => [
172+
{
173+
jsonValue: async () => {
174+
throw new Error('Execution context is not available');
175+
},
176+
},
177+
],
178+
});
179+
const formatter = await ConsoleFormatter.from(message, {
180+
id: 6,
181+
fetchDetailedData: true,
182+
});
183+
const result = formatter.toStringDetailed();
184+
t.assert.snapshot?.(result);
185+
assert.ok(result.includes('<error: Argument 0 is no longer available>'));
186+
});
166187
});
167188
describe('toJSON', () => {
168189
it('formats a console.log message', async () => {

0 commit comments

Comments
 (0)