Skip to content

feat(core): Add safeCallback helper for isolating user-provided callbacks - #23760

Merged
msonnb merged 1 commit into
developfrom
ms/safe-callback-helper
Sep 3, 2026
Merged

feat(core): Add safeCallback helper for isolating user-provided callbacks#23760
msonnb merged 1 commit into
developfrom
ms/safe-callback-helper

Conversation

@msonnb

@msonnb msonnb commented Aug 31, 2026

Copy link
Copy Markdown
Member

Adds safeCallback(message, fn, fallback) helper which runs a user-provided callback, and on a sync throw or async rejection logs message via debug.error and returns fallback(error) instead of propagating.

applyBeforeSendSpanCallback and the undici safeExecute are refactored onto the helper so there is a single implementation.

First step of #23755, next PR will wrap the rest of the user-defined callbacks.

@github-actions

github-actions Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 28.61 kB +0.18% +51 B 🔺
@sentry/browser - with treeshaking flags 26.98 kB +0.2% +53 B 🔺
@sentry/browser - with treeshaking flags tracing without tracing 26.86 kB +0.18% +46 B 🔺
@sentry/browser (incl. Tracing) 48.9 kB +0.08% +39 B 🔺
@sentry/browser (incl. Tracing + Span Streaming) 48.92 kB +0.11% +52 B 🔺
@sentry/browser (incl. Tracing, Profiling) 51.83 kB +0.09% +44 B 🔺
@sentry/browser (incl. Tracing, Replay) 88.41 kB +0.08% +64 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.79 kB +0.06% +40 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 93.11 kB +0.08% +68 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 106.06 kB +0.05% +52 B 🔺
@sentry/browser (incl. Feedback) 46.1 kB +0.12% +54 B 🔺
@sentry/browser (incl. sendFeedback) 33.67 kB +0.14% +46 B 🔺
@sentry/browser (incl. FeedbackAsync) 38.77 kB +0.12% +46 B 🔺
@sentry/browser (incl. Metrics) 29.56 kB +0.18% +51 B 🔺
@sentry/browser (incl. Logs) 29.84 kB +0.15% +42 B 🔺
@sentry/browser (incl. Metrics & Logs) 30.48 kB +0.15% +45 B 🔺
@sentry/react 30.35 kB +0.17% +49 B 🔺
@sentry/react (incl. Tracing) 51.12 kB +0.12% +58 B 🔺
@sentry/vue 35.77 kB +0.11% +39 B 🔺
@sentry/vue (incl. Tracing) 51.17 kB +0.09% +42 B 🔺
@sentry/svelte 28.64 kB +0.17% +47 B 🔺
CDN Bundle 30.4 kB +0.19% +55 B 🔺
CDN Bundle (incl. Tracing) 49.57 kB +0.13% +63 B 🔺
CDN Bundle (incl. Logs, Metrics) 32.63 kB +0.16% +52 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 51.46 kB +0.1% +49 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 73.23 kB +0.08% +53 B 🔺
CDN Bundle (incl. Tracing, Replay) 87.05 kB +0.06% +50 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 88.91 kB +0.05% +42 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 92.98 kB +0.06% +52 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 94.88 kB +0.07% +59 B 🔺
CDN Bundle - uncompressed 90.08 kB +0.15% +132 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 147.69 kB +0.09% +132 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 96.38 kB +0.14% +132 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 153.38 kB +0.09% +132 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 225.54 kB +0.06% +132 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 267.18 kB +0.05% +132 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 272.86 kB +0.05% +132 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 280.88 kB +0.05% +132 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 286.55 kB +0.05% +132 B 🔺
@sentry/nextjs (client) 53.74 kB +0.12% +61 B 🔺
@sentry/sveltekit (client) 49.34 kB +0.11% +54 B 🔺
@sentry/core/server 40.77 kB +0.13% +49 B 🔺
@sentry/core/browser 13.48 kB +0.4% +53 B 🔺
@sentry/node 123.87 kB +0.05% +61 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 81.4 kB - -
@sentry/node - without tracing 88.16 kB +0.07% +57 B 🔺
@sentry/node - without channel injection 103.31 kB +0.07% +64 B 🔺
@sentry/aws-serverless 96.52 kB +0.07% +66 B 🔺
@sentry/cloudflare (withSentry) - minified 201.38 kB +0.09% +169 B 🔺
@sentry/cloudflare (withSentry) 501.16 kB +0.1% +466 B 🔺

View base workflow run

@msonnb

msonnb commented Aug 31, 2026

Copy link
Copy Markdown
Member Author

bugbot run

@msonnb msonnb changed the title ref(core): Add safeCallback helper for isolating user-provided callbacks feat(core): Add safeCallback helper for isolating user-provided callbacks Aug 31, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit bb9a102. Configure here.

return modifiedSpan;
}

if (!hasShownSpanDropWarning) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

unrelated to this PR, but we can possibly remove this warning (safe some bytes), this has been this way for some time 🤔 or at least make it a debug.warn gated by the debug flag so you can shake it out?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah i think we can remove it tbh. has been like this since v9 and the type doesn't allow it anyway.

@msonnb
msonnb force-pushed the ms/safe-callback-helper branch from bb9a102 to 9792842 Compare September 1, 2026 12:17
@msonnb
msonnb marked this pull request as ready for review September 1, 2026 12:20
@msonnb
msonnb requested a review from a team as a code owner September 1, 2026 12:20
@msonnb
msonnb requested review from Lms24, logaretm and stephanie-anderson and removed request for a team and stephanie-anderson September 1, 2026 12:20

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9792842. Configure here.

Comment thread packages/core/src/utils/safeCallback.ts

@Lms24 Lms24 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Had one follow-up suggestion for better tree-shaking but otherwise looks good!

}

function recover<T>(message: string, error: unknown, fallback: (error: unknown) => T): T {
DEBUG_BUILD && debug.error(message, error);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the flag is correct here but the bot review is right: We no longer tree-shake out the actual string passed to this function if DEBUG_BUILD is false. We can still add a ternary to the call-sitest to enable tree shaking, something like

safeCallback(DEBUG_BUILD ? 'full warning/error message' : '', () => {}, () => {})

…backs

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@msonnb
msonnb force-pushed the ms/safe-callback-helper branch from 9792842 to e3f5c00 Compare September 1, 2026 12:42
@msonnb
msonnb merged commit 6011362 into develop Sep 3, 2026
285 of 286 checks passed
@msonnb
msonnb deleted the ms/safe-callback-helper branch September 3, 2026 09:16
msonnb added a commit that referenced this pull request Sep 3, 2026
… as events (#23770)

Wraps `beforeSend`, `beforeSendTransaction`, event processors,
`tracesSampler`, `beforeBreadcrumb`, `beforeSendLog` and
`beforeSendMetric` in the `safeCallback` helper from #23760. A throwing
or rejecting callback no longer escapes into the calling code and is no
longer captured as an `internal` error event; the
event/breadcrumb/log/metric is dropped, a client report is recorded
where a category exists, and the error is logged in debug mode.

part of #23755

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
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.

3 participants