Skip to content

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

Open
msonnb wants to merge 1 commit into
developfrom
ms/safe-callback-helper
Open

feat(core): Add safeCallback helper for isolating user-provided callbacks#23760
msonnb wants to merge 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.6 kB -4.13% -1.23 kB 🔽
@sentry/browser - with treeshaking flags 26.98 kB -3.77% -1.05 kB 🔽
@sentry/browser (incl. Tracing) 48.89 kB +3.48% +1.64 kB 🔺
@sentry/browser (incl. Tracing + Span Streaming) 48.91 kB +3.47% +1.64 kB 🔺
@sentry/browser (incl. Tracing, Profiling) 51.83 kB -0.33% -171 B 🔽
@sentry/browser (incl. Tracing, Replay) 88.41 kB +2.1% +1.82 kB 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.79 kB +2.35% +1.78 kB 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 93.1 kB +2% +1.82 kB 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 106.05 kB +2.02% +2.1 kB 🔺
@sentry/browser (incl. Feedback) 46.09 kB -2.22% -1.04 kB 🔽
@sentry/browser (incl. sendFeedback) 33.66 kB -2.91% -1.01 kB 🔽
@sentry/browser (incl. FeedbackAsync) 38.77 kB -2.55% -1.01 kB 🔽
@sentry/browser (incl. Metrics) 29.55 kB -4.38% -1.35 kB 🔽
@sentry/browser (incl. Logs) 29.84 kB -4.2% -1.31 kB 🔽
@sentry/browser (incl. Metrics & Logs) 30.47 kB -4.22% -1.34 kB 🔽
@sentry/react 30.35 kB -4.04% -1.28 kB 🔽
@sentry/react (incl. Tracing) 51.11 kB +3.23% +1.6 kB 🔺
@sentry/vue 35.76 kB +2.39% +834 B 🔺
@sentry/vue (incl. Tracing) 51.16 kB +3.91% +1.92 kB 🔺
@sentry/svelte 28.63 kB -4.12% -1.23 kB 🔽
CDN Bundle 30.4 kB -4.62% -1.47 kB 🔽
CDN Bundle (incl. Tracing) 49.57 kB +4.27% +2.03 kB 🔺
CDN Bundle (incl. Logs, Metrics) 32.63 kB -2.35% -782 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) 51.46 kB +5.14% +2.51 kB 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 73.23 kB +0.66% +478 B 🔺
CDN Bundle (incl. Tracing, Replay) 87.05 kB +2.19% +1.86 kB 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 88.91 kB +2.79% +2.4 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 92.98 kB +2.17% +1.97 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 94.88 kB +2.79% +2.57 kB 🔺
CDN Bundle - uncompressed 90.08 kB -4.8% -4.54 kB 🔽
CDN Bundle (incl. Tracing) - uncompressed 147.69 kB +3.92% +5.57 kB 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 96.38 kB -2.89% -2.87 kB 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 153.38 kB +4.99% +7.28 kB 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 225.54 kB +0.72% +1.6 kB 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 267.18 kB +2.23% +5.81 kB 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 272.86 kB +2.84% +7.52 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 280.88 kB +2.11% +5.8 kB 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 286.55 kB +2.7% +7.51 kB 🔺
@sentry/nextjs (client) 53.74 kB +3.19% +1.66 kB 🔺
@sentry/sveltekit (client) 49.34 kB +3.43% +1.63 kB 🔺
@sentry/core/server 40.77 kB -37.78% -24.75 kB 🔽
@sentry/core/browser 13.47 kB -74.04% -38.41 kB 🔽
@sentry/node 123.86 kB +3.02% +3.62 kB 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 81.4 kB added added
@sentry/node - without tracing 88.15 kB +5.34% +4.46 kB 🔺
@sentry/aws-serverless 96.51 kB +4.51% +4.16 kB 🔺
@sentry/cloudflare (withSentry) - minified 201.37 kB -7.91% -17.28 kB 🔽
@sentry/cloudflare (withSentry) 501.14 kB -7% -37.7 kB 🔽
@sentry/browser - with treeshaking flags tracing without tracing 26.86 kB added added
@sentry/node - without channel injection 103.29 kB added added

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.

…backs

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


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Debug strings skip tree-shaking gate

Low Severity

Flagged this because the review rules require DEBUG_BUILD && debug.error(...) so message text can be tree-shaken from production browser bundles. safeCallback takes message as a live argument, so the beforeSendSpan error string is no longer behind that gate and now ships in production.

Additional Locations (1)
Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 9792842. Configure here.

@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' : '', () => {}, () => {})

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