Chrome DevTools for coding agents
fix(console): stop resolving a cyclic Error.cause chain after 10 causes (#3338)
Victor Hugo Macedo Paes Leme172656ab38a4
diff --git a/src/devtools/DevtoolsUtils.ts b/src/devtools/DevtoolsUtils.tsindex 6ff035af..cd88c6e3 100644--- a/src/devtools/DevtoolsUtils.ts+++ b/src/devtools/DevtoolsUtils.ts@@ -229,6 +229,13 @@ export type ExceptionDetailsLike =export type StackTraceLike =Protocol.Runtime.StackTrace | DevTools.Protocol.Runtime.StackTrace;+/**+ * Maximum number of `cause` links resolved for one error. A page can create a+ * cyclic chain (`error.cause = error`), and CDP returns a new object id on+ * every lookup of the same object, so a cycle cannot be detected by id.+ */+const MAX_CAUSE_DEPTH = 10;+/*** Constructed from Runtime.ExceptionDetails of an uncaught error.*@@ -262,6 +269,13 @@ export class SymbolizedError {resolvedStackTraceForTesting?: DevTools.StackTrace.StackTrace.StackTrace;resolvedCauseForTesting?: SymbolizedError;}): Promise<SymbolizedError> {+ return await SymbolizedError.#fromDetails(opts, 0);+ }++ static async #fromDetails(+ opts: Parameters<typeof SymbolizedError.fromDetails>[0],+ causeDepth: number,+ ): Promise<SymbolizedError> {const message = SymbolizedError.#getMessage(opts.details);if (!opts.includeStackAndCause || (!opts.devTools && !opts.session)) {return new SymbolizedError(@@ -291,7 +305,7 @@ export class SymbolizedError {let cause: SymbolizedError | undefined;if (opts.resolvedCauseForTesting) {cause = opts.resolvedCauseForTesting;- } else if (opts.details.exception) {+ } else if (opts.details.exception && causeDepth < MAX_CAUSE_DEPTH) {try {const causeRemoteObj = await SymbolizedError.#lookupCause(opts.devTools,@@ -300,12 +314,15 @@ export class SymbolizedError {opts.session,);if (causeRemoteObj) {- cause = await SymbolizedError.fromError({- devTools: opts.devTools,- error: causeRemoteObj,- targetId: opts.targetId,- session: opts.session,- });+ cause = await SymbolizedError.#fromError(+ {+ devTools: opts.devTools,+ error: causeRemoteObj,+ targetId: opts.targetId,+ session: opts.session,+ },+ causeDepth + 1,+ );}} catch {// Ignore@@ -320,6 +337,13 @@ export class SymbolizedError {devTools?: TargetUniverse;session?: CDPSession;}): Promise<SymbolizedError> {+ return await SymbolizedError.#fromError(opts, 0);+ }++ static async #fromError(+ opts: Parameters<typeof SymbolizedError.fromError>[0],+ causeDepth: number,+ ): Promise<SymbolizedError> {const details = await SymbolizedError.#getExceptionDetails(opts.devTools,opts.error,@@ -327,13 +351,16 @@ export class SymbolizedError {opts.session,);if (details) {- return await SymbolizedError.fromDetails({- details,- devTools: opts.devTools,- targetId: opts.targetId,- session: opts.session,- includeStackAndCause: true,- });+ return await SymbolizedError.#fromDetails(+ {+ details,+ devTools: opts.devTools,+ targetId: opts.targetId,+ session: opts.session,+ includeStackAndCause: true,+ },+ causeDepth,+ );}return new SymbolizedError(diff --git a/tests/devtools/DevtoolsUtils.test.ts b/tests/devtools/DevtoolsUtils.test.tsindex 0dc37710..0d1e6f27 100644--- a/tests/devtools/DevtoolsUtils.test.ts+++ b/tests/devtools/DevtoolsUtils.test.ts@@ -14,6 +14,7 @@ import {SymbolizedError,} from '../../src/devtools/DevtoolsUtils.js';import {DevTools, type Protocol} from '../../src/third_party/index.js';+import {createMockCDPSession} from '../mocks.js';import {serverHooks} from '../server.js';import {html, withBrowser} from '../utils.js';@@ -396,6 +397,57 @@ describe('SymbolizedError', () => {assert.strictEqual(error.cause, cause);assert.strictEqual(error.cause?.message, 'cause message');});++ it('stops resolving a cyclic cause chain after 10 causes', async () => {+ const session = createMockCDPSession();+ const send: sinon.SinonStub = session.send;+ let lookups = 0;+ // CDP returns a new object id on every lookup of the same error.+ const selfCausedError = (): Protocol.Runtime.RemoteObject => ({+ type: 'object',+ subtype: 'error',+ className: 'Error',+ description: 'Error: self-cause',+ objectId: `error-${++lookups}`,+ });+ send.withArgs('Runtime.getProperties').callsFake(async () => {+ // Ends an unbounded chain so that the test fails instead of hanging.+ if (lookups > 100) {+ throw new Error('The cause chain is not bounded.');+ }+ return {+ result: [+ {+ name: 'cause',+ configurable: true,+ enumerable: true,+ value: selfCausedError(),+ },+ ],+ };+ });+ send.withArgs('Runtime.getExceptionDetails').callsFake(async () => ({+ exceptionDetails: exceptionDetails({+ text: 'Error: self-cause',+ exception: selfCausedError(),+ }),+ }));++ const error = await SymbolizedError.fromDetails({+ details: exceptionDetails({exception: selfCausedError()}),+ targetId: '',+ session,+ includeStackAndCause: true,+ });++ let causes = 0;+ for (let cause = error.cause; cause; cause = cause.cause) {+ assert.strictEqual(cause.message, 'Error: self-cause');+ causes++;+ }+ assert.strictEqual(causes, 10);+ sinon.assert.callCount(send.withArgs('Runtime.getProperties'), 10);+ });});describe('fromError', () => {