Skip to content

🐛 Harden debugger error formatting for hostile values#4740

Open
watson wants to merge 3 commits into
mainfrom
watson/support-non-error-thrown
Open

🐛 Harden debugger error formatting for hostile values#4740
watson wants to merge 3 commits into
mainfrom
watson/support-non-error-thrown

Conversation

@watson
Copy link
Copy Markdown
Collaborator

@watson watson commented Jun 7, 2026

Motivation

Live Debugger runs inside instrumented application control flow, so debugger formatting must tolerate arbitrary JavaScript values without throwing while building snapshots.

JavaScript can throw any value, not just Error instances. Debugger conditions, templates, and captures can also encounter cross-realm Errors, hostile getters, or values that cannot be coerced to strings.

Changes

This PR hardens debugger error formatting and capture behavior:

  • normalize debugger throwables from unknown values instead of assuming Error
  • preserve message and parsed stacktrace for Error values, including cross-realm Errors
  • format non-Error values safely and keep an empty stacktrace for the current Live Debugger UI contract
  • safely format condition and template evaluation errors from unknown thrown values
  • inject a scoped $dd_format_error helper into generated template code catch blocks
  • capture cross-realm Errors and Error properties whose getters throw
  • add shared browser-core helpers for safe String() and sanitized JSON formatting

Test instructions

Run the focused unit tests:

yarn test:unit --spec packages/browser-debugger/src/domain/api.spec.ts --spec packages/browser-debugger/src/domain/capture.spec.ts --spec packages/browser-debugger/src/domain/condition.spec.ts --spec packages/browser-debugger/src/domain/template.spec.ts --spec packages/browser-debugger/src/domain/expression.spec.ts --spec packages/browser-debugger/src/domain/probes.spec.ts --spec packages/browser-core/src/domain/error/error.spec.ts --spec packages/browser-core/src/tools/serialisation/stringify.spec.ts

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@watson watson requested review from a team as code owners June 7, 2026 05:15
Copy link
Copy Markdown
Collaborator Author

watson commented Jun 7, 2026

@cit-pr-commenter-54b7da
Copy link
Copy Markdown

cit-pr-commenter-54b7da Bot commented Jun 7, 2026

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 171.88 KiB 171.90 KiB +20 B +0.01%
Rum Profiler 7.88 KiB 7.88 KiB 0 B 0.00%
Rum Recorder 21.21 KiB 21.21 KiB 0 B 0.00%
Logs 54.34 KiB 54.36 KiB +20 B +0.04%
Rum Slim 129.72 KiB 129.74 KiB +20 B +0.02%
Worker 22.96 KiB 22.96 KiB 0 B 0.00%

@datadog-prod-us1-5
Copy link
Copy Markdown

datadog-prod-us1-5 Bot commented Jun 7, 2026

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 72.22%
Overall Coverage: 76.77% (-0.04%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: f1febfb | Docs | Datadog PR Page | Give us feedback!

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 03b6d4c0c6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-debugger/src/domain/api.ts Outdated
Comment thread packages/browser-debugger/src/domain/api.ts Outdated
@watson watson changed the base branch from main to graphite-base/4740 June 7, 2026 05:21
@watson watson force-pushed the watson/support-non-error-thrown branch from 03b6d4c to 9c9b8ac Compare June 7, 2026 05:21
@watson watson changed the base branch from graphite-base/4740 to watson/cleanup-tests June 7, 2026 05:21
@watson watson force-pushed the watson/support-non-error-thrown branch from 9c9b8ac to f32c7a4 Compare June 7, 2026 05:43
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f32c7a4f83

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-debugger/src/domain/api.ts Outdated
Base automatically changed from watson/cleanup-tests to main June 8, 2026 06:03
@watson watson force-pushed the watson/support-non-error-thrown branch 2 times, most recently from ae7762d to 9b05c9e Compare June 8, 2026 12:21
@watson watson changed the title 🐛 Handle non-Error debugger throw values 🐛 Harden debugger error formatting for hostile values Jun 8, 2026
@watson watson force-pushed the watson/support-non-error-thrown branch from 9b05c9e to f6260d9 Compare June 8, 2026 12:23
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f6260d9616

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-core/src/tools/serialisation/stringify.ts
Comment thread packages/browser-debugger/src/domain/template.ts Outdated
@watson watson force-pushed the watson/support-non-error-thrown branch from f6260d9 to b4c641f Compare June 8, 2026 12:45
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b4c641f43c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if (!fn) {
// eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval
fn = new Function('$dd_inspect', ...contextKeys, fnBodyTemplate) as (...args: any[]) => unknown[]
fn = new Function('$dd_inspect', '$dd_format_error', ...contextKeys, fnBodyTemplate) as (
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid shadowing the template error helper

When the instrumented function has an argument or local named $dd_format_error and any template segment throws during evaluation, this helper parameter is duplicated by ...contextKeys and the customer value shadows the formatter passed at call time. Because debugger expression identifiers can start with $, the catch generated in compileSegments() then calls the customer value as a function and the whole template falls back to a TypeError instead of reporting the original segment error; use a collision-free binding or filter/rename helper parameters before appending context keys.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'll fix this in another PR

@watson watson force-pushed the watson/support-non-error-thrown branch from b4c641f to eb2aef1 Compare June 8, 2026 13:15
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: eb2aef196c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/browser-debugger/src/domain/api.ts Outdated
Comment thread packages/browser-debugger/src/domain/condition.ts Outdated
Comment thread packages/browser-debugger/src/domain/template.ts Outdated
JavaScript allows throwing arbitrary values, but the debugger hook typed thrown
values as Error and read message/stack unconditionally.

Type thrown values as unknown, safely format non-Error throwables, and keep an
empty stacktrace for the current Live Debugger UI contract.

Reuse the shared browser-core isError helper for cross-realm Error detection and
make it non-throwing for hostile toStringTag getters.

Add unit tests covering thrown strings, cross-realm Errors, and values that
cannot be coerced to strings.
@watson watson force-pushed the watson/support-non-error-thrown branch from eb2aef1 to f4a6786 Compare June 8, 2026 17:57
@watson watson force-pushed the watson/support-non-error-thrown branch from f4a6786 to 753205e Compare June 8, 2026 18:00
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 753205ed3b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +229 to 230
if (isError(obj)) {
return captureError(obj, depth, maxReferenceDepth, maxCollectionSize, maxFieldCount, maxLength, ctx)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Guard Error stack/cause captures

When this branch now accepts Error-like objects via isError() (for example { [Symbol.toStringTag]: 'Error', stack: new Proxy({}, { ownKeys() { throw ... } }) }), captureError() only guards the initial stack/cause getter and then calls captureValue() outside that guard. If the returned stack/cause value throws while being captured, the debugger hook can still throw; before this change the same Error-like object used the generic property path, whose per-property try covered the nested capture as well.

Useful? React with 👍 / 👎.

Comment thread packages/browser-core/src/domain/error/error.ts Outdated
Comment thread packages/browser-debugger/src/domain/capture.ts
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.

1 participant