Skip to content

fix: guard error handler against non-object thrown values#969

Closed
AI-Reviewer-QS wants to merge 1 commit intoChromeDevTools:mainfrom
AI-Reviewer-QS:fix/error-handler-primitive-values
Closed

fix: guard error handler against non-object thrown values#969
AI-Reviewer-QS wants to merge 1 commit intoChromeDevTools:mainfrom
AI-Reviewer-QS:fix/error-handler-primitive-values

Conversation

@AI-Reviewer-QS
Copy link
Copy Markdown

Summary

  • The in operator throws a TypeError when its right-hand operand is not an object
  • In the tool execution error handler, both 'message' in err and 'cause' in err crash if the caught value is a primitive (null, string, number, undefined)
  • Added typeof err === 'object' checks before using the in operator to ensure primitive thrown values are safely converted to strings

Reproduction

try {
  throw null; // or throw "some string", throw 42
} catch (err) {
  'message' in err; // TypeError: Cannot use 'in' operator to search for 'message' in null
}

Test plan

  • Verify tool error handling with throw null
  • Verify tool error handling with throw "string"
  • Verify tool error handling with throw 42
  • Verify tool error handling with standard throw new Error("msg") still works
  • Verify error with cause property still includes cause message

The `in` operator throws a TypeError when its right-hand operand is not
an object. In the tool execution error handler, both `'message' in err`
and `'cause' in err` will crash if the caught value is a primitive
(null, string, number, undefined).

Add `typeof err === 'object'` checks before using the `in` operator to
ensure primitive thrown values are safely converted to strings.
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Feb 16, 2026

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
Copy link
Copy Markdown
Collaborator

OrKoN commented Feb 16, 2026

Is there a scenario where a non Error object is thrown?

@Lightning00Blade
Copy link
Copy Markdown
Collaborator

We can easily enforce this with '@typescript-eslint/only-throw-error': 'error', rather then having more code handling.

@OrKoN
Copy link
Copy Markdown
Collaborator

OrKoN commented Feb 18, 2026

We can easily enforce this with '@typescript-eslint/only-throw-error': 'error', rather then having more code handling.

would not help for errors thrown by dependencies

@OrKoN
Copy link
Copy Markdown
Collaborator

OrKoN commented Feb 18, 2026

I think this is redundant for now, please let us know if you have a scenario where a non-object is thrown.

@OrKoN OrKoN closed this Feb 18, 2026
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.

4 participants