Fix: make formatError's fallback stringification total - #824
Open
AmaadMartin wants to merge 2 commits into
Open
Conversation
added 2 commits
August 8, 2026 14:06
baseMessage ended in `String(err)`, which throws for a null-prototype object or a value with a throwing `toString`, and reduces a plain object to `[object Object]`. Both contradict the documented promise that `formatError` never throws. Serialize a record with `JSON.stringify` (truncated at the existing response-body limit), keep the `toString` fall-through for a value with no serializable own fields, and wrap the whole helper in one try/catch that degrades to `<unstringifiable value>`.
The try/catch sat in baseMessage, but extractHttpDetails and formatErrorRecursive read `status`, `code`, `response`, `statusText` and `cause` before baseMessage runs. A throwing getter therefore still escaped, so the documented "never throws" promise stayed false. Move the guard to formatError, which covers every read on the path, and inline the single-use EMPTY_JSON_OBJECT constant. Add a test for a throwing `status` getter to pin the new placement.
AmaadMartin
pushed a commit
that referenced
this pull request
Aug 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
No issue.
core/src/utils/error_utils.tslanded in google#527.Problem: The
formatErrordoc comment promises that it never throws, but the function had no guard.String(err)inbaseMessagethrows on a null-prototype object and on any value whosetoStringthrows, andextractHttpDetailsthrows on a value with a throwingstatus,code,responseorstatusTextgetter.formatErrorbacks the MCP transport error handler (mcp_session_manager.ts:22), so such a throw escapes into an async I/O callback and the error handler becomes the crash.String()also reduces{code: -32601, message: 'Method not found'}to'[object Object]', discarding every field.Solution:
baseMessagenow serializes a record withJSON.stringifyand truncates it with the existingtruncateBody. A plain object therefore surfaces its fields under the same 1000-character bound as an HTTP response body. TheString(err)fall-through stays for a value with no serializable own fields, so a class instance with a usefultoStringis unchanged. Onetry/catchat the exported boundary returns<unstringifiable value>, mirroringsafeJsonSerializeincore/src/telemetry/tracing.ts.Scope notes:
formatError, not onbaseMessage. Every read that can throw is then covered, including the getter reads inextractHttpDetailsand thecauseread informatErrorRecursive, which both run beforebaseMessage. A guard insidebaseMessagewould leave those uncovered and the documented promise still false.formatErroris internal. It is not exported fromcore/src/index.tsorcore/src/common.ts. No export, signature or type changed.core/src/utils/error_utils.tsorformatError. I also read the file lists of the four MCP-adjacent PRs (Feat: add an optional headerProvider to MCPToolset #665, Fix: declare @modelcontextprotocol/sdk in the workspace root manifest #707, Feat: expose an ADK agent as an MCP server (toMcpServer) #580, Feat: port ToolboxToolset from adk-python (MCP Toolbox for Databases) #583); none touch it.Testing Plan
Unit Tests:
Ten cases added to
core/test/utils/error_utils_test.ts. All 27 existing cases are unedited and still pass: 37 passed.Coverage of
core/src/utils/error_utils.ts: 100% of statements, branches, functions and lines.Proof each test can fail. I ran four mutations and recorded the failures.
Mutation A — revert
baseMessageto its original three-line body:expected '[object Object]' to contain 'errorCode'expected '[object Object]' to be '<unstringifiable value>'expected '[object Object]' to be '<unstringifiable value>'expected '<unstringifiable value>' to contain 'no prototype'expected '[object Object]' to contain '... [truncated]'Mutation B — delete the
try/catchfromformatError:toStringexpected [Function] to not throw an error but 'Error: toString exploded' was thrownexpected [Function] to not throw an error but 'Error: getter exploded' was thrownexpected [Function] to not throw an error but 'TypeError: Converting circular structure...' was thrownDo not know how to serialize a BigIntMutation C — drop the
typeof json === 'string'guard:toJSON returns nothingfails withexpected '<unstringifiable value>' to be '[object Object]'.Mutation D — drop the
json !== '{}'guard:custom toStringfails withexpected '{}' to be 'ErrorLike: connection reset', andthrowing toStringfails withexpected '{}' to be '<unstringifiable value>'.The tenth case,
formatError(42), survives all four. It is a regression guard for the primitive path, not a failing-first test.Manual End-to-End (E2E) Tests:
Not applicable. The change is one utility module, and the unit test file drives it through the public
formatError. To reproduce the old behaviour in plain node:Other checks on the pushed commit:
npx eslint core/src/utils/error_utils.ts core/test/utils/error_utils_test.ts— clean.npx prettier --checkon both files — clean.npm run ts:checkreports 292 errors on this branch and 292 onmain. None are in either file I touched.Checklist