Skip to content

fix: normalize non-Error values thrown by tool handlers - #3068

Merged
OrKoN merged 2 commits into
ChromeDevTools:mainfrom
costajohnt:fix/2951-non-error-throws
Nov 3, 2026
Merged

OrKoN merged 2 commits into
ChromeDevTools:mainfrom
costajohnt:fix/2951-non-error-throws

Conversation

@costajohnt

@costajohnt costajohnt commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Refs #2951

#3127 fixed the evaluate_script side of #2951 with in-page normalization, so this PR now only covers the shared ToolHandler path, which still passes the raw thrown value to setError.

When a tool handler throws something that isn't an Error, setError (which expects an Error) stores it as-is and the response reads .message. A thrown string comes back with an empty message, and a falsy value like 0 leaves response.error falsy, so the call isn't flagged as an error at all.

  • src/utils/errorHandling.ts: new toError(err: unknown): Error next to getErrorMessage.
  • src/ToolHandler.ts: both setError call sites use toError(err). The outer catch also guards its 'message' in err / 'cause' in err checks, since in on a primitive throws a TypeError and the value would escape the catch instead of being reported.

Tests: a parametrized tests/ToolHandler.test.ts case for a handler that throws 'str-thrown' and 0. Both fail with the src changes reverted. tsc --noEmit, eslint and prettier clean on the changed files.

@google-cla

google-cla Bot commented Oct 11, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@OrKoN

OrKoN commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Closing due to missing CLA.

@OrKoN OrKoN closed this Oct 14, 2026
@costajohnt

Copy link
Copy Markdown
Contributor Author

I've signed the CLA now and the cla/google check is passing. Could you reopen this when you get a chance? Thanks!

@OrKoN OrKoN reopened this Oct 14, 2026

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please rebase?

@costajohnt
costajohnt force-pushed the fix/2951-non-error-throws branch from ffa627d to fee8965 Compare October 17, 2026 03:22
@OrKoN
OrKoN self-requested a review October 17, 2026 04:50
@costajohnt

Copy link
Copy Markdown
Contributor Author

Rebased onto main. The fix now sits in the two catch blocks of #runBrowserlessTool and #runBrowserTool, since handle() was split upstream.

@OrKoN OrKoN left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the following and rebase:

  1. In src/ToolHandler.ts, replace the two inline err instanceof Error ? err : new Error(String(err)) expressions with one small helper (for example toError(err: unknown): Error in src/utils/errorHandling.ts, next to getErrorMessage). The same expression now appears twice and will be needed by other catch sites.

  2. In src/tools/script.ts, extend the in-page wrapper comment or the PR description to state that a plain object thrown from another realm (for example an iframe's {code: 42}) has a foreign Object.prototype, is rethrown unchanged, and still reports Error: Object. The current comment implies all plain objects are covered.

Nit (non-blocking): in tests/ToolHandler.test.ts, the mockContext.ensureBrowser.resolves(getMockBrowser(...)) setup is not needed for a handler that throws before touching the browser; dropping it shortens the test.

@costajohnt
costajohnt force-pushed the fix/2951-non-error-throws branch from fee8965 to 48f0613 Compare October 19, 2026 23:12
@costajohnt costajohnt changed the title fix: report non-Error values thrown by evaluate_script fix: normalize non-Error values thrown by tool handlers Oct 19, 2026
@costajohnt

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Rebased onto main and narrowed this down, since #3127 landed today and already fixes the evaluate_script side of #2951. I dropped my script.ts change along with the iframe-realm note, since that described my in-page wrapper, which is gone now.

What's left is the ToolHandler part, which upstream still has: setError(err) gets the raw value, so a string thrown by any tool gives an empty message and a thrown 0 isn't flagged as an error. I added toError in src/utils/errorHandling.ts as you suggested and used it at both catch sites, and retitled the PR to match.

On the test nit: I tried dropping the ensureBrowser setup, but on current main the handler path touches the browser before the tool runs, so the test fails with Cannot read properties of undefined (reading 'process') without it. I kept it for that reason. Happy to restructure the test if you'd prefer a different setup.

When a tool handler threw a non-Error value, ToolHandler passed it
straight to McpResponse.setError, which expects an Error. A thrown
string produced an undefined message, and a falsy value such as 0 was
not reported as an error at all.

Add a toError helper next to getErrorMessage and use it at both
setError call sites. Also guard the outer catch: `'cause' in err`
throws a TypeError for a primitive, so one reaching that catch escaped
it instead of being reported.

Refs #2951
@costajohnt
costajohnt force-pushed the fix/2951-non-error-throws branch from 48f0613 to b46ef59 Compare October 24, 2026 18:50
@costajohnt

Copy link
Copy Markdown
Contributor Author

Rebased onto main. toError now sits next to getErrorMessage and both setError sites use it.

On the script.ts point: that comment was about my earlier wrapper, which #3127 replaced, so this PR doesn't touch script.ts anymore. In the current wrapper a cross-realm {code: 42} has no string message, so it gets JSON-stringified and reports Error: {"code":42} instead of being rethrown.

On the nit: I tried dropping the ensureBrowser stub, but the handler runs after the browser is ensured, so the test fails without it. Left it in.

@OrKoN
OrKoN self-requested a review November 3, 2026 08:37
@OrKoN
OrKoN enabled auto-merge November 3, 2026 08:37
@OrKoN
OrKoN added this pull request to the merge queue Nov 3, 2026
Merged via the queue into ChromeDevTools:main with commit 535a3dc Nov 3, 2026
14 checks passed
Lightning00Blade pushed a commit to jaeminbird/chrome-devtools-mcp that referenced this pull request Nov 11, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.13.0](ChromeDevTools/chrome-devtools-mcp@chrome-devtools-mcp-v1.12.0...chrome-devtools-mcp-v1.13.0)
(2026-11-11)


### 🎉 Features

* add logpoints built on the DevTools BreakpointManager
([ChromeDevTools#3211](ChromeDevTools#3211))
([0877ee0](ChromeDevTools@0877ee0))
* add page-scoped network mocks
([ChromeDevTools#3384](ChromeDevTools#3384))
([1d692d3](ChromeDevTools@1d692d3))
* support upstream network mock responses
([ChromeDevTools#3420](ChromeDevTools#3420))
([c4bf342](ChromeDevTools@c4bf342))


### 🛠️ Fixes

* **config:** accept config files with a UTF-8 BOM
([ChromeDevTools#3365](ChromeDevTools#3365))
([63170b1](ChromeDevTools@63170b1))
* **console:** stop resolving a cyclic Error.cause chain after 10 causes
([ChromeDevTools#3338](ChromeDevTools#3338))
([172656a](ChromeDevTools@172656a))
* disable set_logpoints when --no-javascript-evaluation is set
([ChromeDevTools#3415](ChromeDevTools#3415))
([11fcc59](ChromeDevTools@11fcc59)),
closes
[ChromeDevTools#3414](ChromeDevTools#3414)
* **extensions:** say list_extensions only lists unpacked extensions
([ChromeDevTools#3433](ChromeDevTools#3433))
([1383bb9](ChromeDevTools@1383bb9))
* ignore an invalid cached version in the update check
([ChromeDevTools#3392](ChromeDevTools#3392))
([3a9f378](ChromeDevTools@3a9f378))
* **input:** validate press_key and submitKey parameters in Zod schema
([ChromeDevTools#3382](ChromeDevTools#3382))
([375a269](ChromeDevTools@375a269))
* **network:** start an empty request list for navigations without a
request
([ChromeDevTools#3357](ChromeDevTools#3357))
([b701776](ChromeDevTools@b701776))
* normalize non-Error values thrown by tool handlers
([ChromeDevTools#3068](ChromeDevTools#3068))
([535a3dc](ChromeDevTools@535a3dc))
* **performance:** error when performance_stop_trace is called on a
non-recording page
([ChromeDevTools#2971](ChromeDevTools#2971))
([ChromeDevTools#3117](ChromeDevTools#3117))
([1ab630a](ChromeDevTools@1ab630a))
* **plugin:** launch the Claude Code MCP server with npx --prefix
([ChromeDevTools#3353](ChromeDevTools#3353))
([a24334d](ChromeDevTools@a24334d))
* preserve task serialization on request cancellation
([ChromeDevTools#3079](ChromeDevTools#3079))
([03fd4e8](ChromeDevTools@03fd4e8))
* report page loads that keep the URL
([ChromeDevTools#3395](ChromeDevTools#3395))
([3cc38bb](ChromeDevTools@3cc38bb))
* save binary request bodies byte for byte
([ChromeDevTools#3404](ChromeDevTools#3404))
([e3eb901](ChromeDevTools@e3eb901))
* suggest --headless when Chrome cannot find an X server
([ChromeDevTools#3428](ChromeDevTools#3428))
([8b0856a](ChromeDevTools@8b0856a))
* **url:** unwrap view-source: before all scheme checks in validateUrl
([ChromeDevTools#3335](ChromeDevTools#3335))
([7275268](ChromeDevTools@7275268))
* **wait:** explain whitespace-sensitive text matching
([ChromeDevTools#3350](ChromeDevTools#3350))
([81a0379](ChromeDevTools@81a0379))


### 📄 Documentation

* add Pi client configuration
([ChromeDevTools#3364](ChromeDevTools#3364))
([bc0d93e](ChromeDevTools@bc0d93e))
* clarify custom browser launch and connection options
([ChromeDevTools#3386](ChromeDevTools#3386))
([7e10c08](ChromeDevTools@7e10c08))
* **input:** note that press_key cannot drive native <select>
popups on macOS
([ChromeDevTools#3288](ChromeDevTools#3288))
([8e79bd0](ChromeDevTools@8e79bd0))


### ⚡ Performance

* **memory:** drop async parents of request initiators in
NetworkCollector
([ChromeDevTools#3345](ChromeDevTools#3345))
([ca6f834](ChromeDevTools@ca6f834)),
closes
[ChromeDevTools#3344](ChromeDevTools#3344)


### 🏗️ Refactor

* re-export only used Zod runtime members from third_party barrel
([ChromeDevTools#3439](ChromeDevTools#3439))
([6b056c8](ChromeDevTools@6b056c8))
* reuse Zod schemas and validate integer viewport dimensions
([ChromeDevTools#3367](ChromeDevTools#3367))
([3c609da](ChromeDevTools@3c609da))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: Alex Rudenko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants