Skip to content

Chrome DevTools for coding agents

fix: improve error handling for console messages (#844)

Alex Rudenkodc43ede1f203
diff --git a/src/McpResponse.ts b/src/McpResponse.ts
index 43a26b68..a00f2887 100644
--- a/src/McpResponse.ts
+++ b/src/McpResponse.ts
@@ -337,7 +337,7 @@ export class McpResponse implements Response {
const devTools = context.getDevToolsUniverse();
return await ConsoleFormatter.from(consoleMessage, {
id: consoleMessageStableId,
- fetchDetailedData: true,
+ fetchDetailedData: false,
devTools: devTools ?? undefined,
});
}
diff --git a/src/formatters/ConsoleFormatter.ts b/src/formatters/ConsoleFormatter.ts
index 366f1a23..4e0fa74f 100644
--- a/src/formatters/ConsoleFormatter.ts
+++ b/src/formatters/ConsoleFormatter.ts
@@ -50,14 +50,24 @@ export class ConsoleFormatter {
}
this.#resolvedArgs = await Promise.all(
- this.#msg.args().map(arg => arg.jsonValue()),
+ this.#msg.args().map(async (arg, i) => {
+ try {
+ return await arg.jsonValue();
+ } catch {
+ return `<error: Argument ${i} is no longer available>`;
+ }
+ }),
);
if (devTools) {
- this.#resolvedStackTrace = await createStackTraceForConsoleMessage(
- devTools,
- this.#msg,
- );
+ try {
+ this.#resolvedStackTrace = await createStackTraceForConsoleMessage(
+ devTools,
+ this.#msg,
+ );
+ } catch {
+ // ignore
+ }
}
}
diff --git a/tests/formatters/ConsoleFormatter.test.js.snapshot b/tests/formatters/ConsoleFormatter.test.js.snapshot
index 76bbb0a7..00520af7 100644
--- a/tests/formatters/ConsoleFormatter.test.js.snapshot
+++ b/tests/formatters/ConsoleFormatter.test.js.snapshot
@@ -44,3 +44,10 @@ Message: log> Processing file:
### Arguments
Arg #0: file.txt
`;
+
+exports[`ConsoleFormatter > toStringDetailed > handles \"Execution context is not available\" error in args 1`] = `
+ID: 6
+Message: log> Processing file:
+### Arguments
+Arg #0: <error: Argument 0 is no longer available>
+`;
diff --git a/tests/formatters/ConsoleFormatter.test.ts b/tests/formatters/ConsoleFormatter.test.ts
index 985bf013..4720ff61 100644
--- a/tests/formatters/ConsoleFormatter.test.ts
+++ b/tests/formatters/ConsoleFormatter.test.ts
@@ -163,6 +163,27 @@ describe('ConsoleFormatter', () => {
).toStringDetailed();
t.assert.snapshot?.(result);
});
+
+ it('handles "Execution context is not available" error in args', async t => {
+ const message = createMockMessage({
+ type: () => 'log',
+ text: () => 'Processing file:',
+ args: () => [
+ {
+ jsonValue: async () => {
+ throw new Error('Execution context is not available');
+ },
+ },
+ ],
+ });
+ const formatter = await ConsoleFormatter.from(message, {
+ id: 6,
+ fetchDetailedData: true,
+ });
+ const result = formatter.toStringDetailed();
+ t.assert.snapshot?.(result);
+ assert.ok(result.includes('<error: Argument 0 is no longer available>'));
+ });
});
describe('toJSON', () => {
it('formats a console.log message', async () => {