Repository navigation
Extract diagnostic info from Safari CustomEvent rejections - #8173
Conversation
Fixes #8172 Fixes COMPILER-EXPLORER-DXM Safari sometimes rejects promises with CustomEvent/Event objects instead of Error objects. When JSON.stringify() is called on these Event objects, it returns '{}' because Event properties are non-enumerable. This fix extracts useful diagnostic properties (type, target, detail) from Event/CustomEvent objects before creating the error message, providing actionable information instead of empty objects. Changes: - Add isEventLike() type guard to detect Event/CustomEvent objects - Add formatEventRejection() to extract meaningful Event properties - Refactor unhandledrejection handler with cleaner ternary chain - Use Object.assign() for type-safe property addition - Add references to GitHub issue and related Sentry SDK issue 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
Pull Request Overview
Improves Safari-specific error handling by extracting meaningful diagnostic information from CustomEvent/Event objects that are rejected as promises, replacing empty object serialization with descriptive error messages.
- Enhanced unhandled rejection handler to detect and format Event/CustomEvent objects
- Added type guards and formatting functions for better error diagnostics
- Refactored error construction using modern TypeScript patterns
| } | ||
|
|
||
| function isEventLike(value: unknown): value is Event { | ||
| return value instanceof Event || value?.constructor?.name === 'Event' || value?.constructor?.name === 'CustomEvent'; |
There was a problem hiding this comment.
The constructor name check could be bypassed by objects with spoofed constructor names. Consider adding additional property checks like 'type' in value && 'target' in value to verify Event-like structure more reliably.
| return value instanceof Event || value?.constructor?.name === 'Event' || value?.constructor?.name === 'CustomEvent'; | |
| return ( | |
| value instanceof Event || | |
| (typeof value === 'object' && | |
| value !== null && | |
| 'type' in value && | |
| 'target' in value) | |
| ); |
| let message = `Event rejection: type="${evt.type}", target="${targetName}"`; | ||
|
|
||
| if ('detail' in evt && evt.detail !== undefined) { | ||
| message += `, detail=${JSON.stringify(evt.detail)}`; |
There was a problem hiding this comment.
JSON.stringify on evt.detail could throw if the detail contains circular references or non-serializable values. Consider wrapping in a try-catch or using a safer serialization method.
| message += `, detail=${JSON.stringify(evt.detail)}`; | |
| let detailStr: string; | |
| try { | |
| detailStr = JSON.stringify(evt.detail); | |
| } catch (err) { | |
| detailStr = '[Unserializable detail]'; | |
| } | |
| message += `, detail=${detailStr}`; |
|
|
||
| // Preserve original reason for debugging | ||
| (reason as any).originalReason = event.reason; | ||
| reason = Object.assign(new Error(errorMessage), {originalReason: event.reason}); |
There was a problem hiding this comment.
[nitpick] Using Object.assign to add properties to Error objects can be less clear than explicit property assignment. Consider using (reason as any).originalReason = event.reason for better readability and consistency with the removed code pattern.
| reason = Object.assign(new Error(errorMessage), {originalReason: event.reason}); | |
| reason = new Error(errorMessage); | |
| (reason as any).originalReason = event.reason; |
|
Thanks for the review, @copilot! Let me address each suggestion: ✅ Suggestion #2: Protect
|
Addresses Copilot review feedback: Wrap JSON.stringify(evt.detail) in try-catch to prevent the error handler from throwing on circular references, BigInts, or other non-serializable values. Uses '[Unserializable]' fallback if stringification fails. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
|
This is now live |
Issues attributed to commits in this pull requestThis pull request was merged and Sentry observed the following issues:
|
Fixes Sentry [COMPILER-EXPLORER-EHY](https://compiler-explorer.sentry.io/issues/COMPILER-EXPLORER-EHY) ("Error: Non-Error rejection: {}", 351 events in 30 days) and its sibling [COMPILER-EXPLORER-EJ9](https://compiler-explorer.sentry.io/issues/COMPILER-EXPLORER-EJ9) ("Non-Error rejection: undefined", 5.8k events in total). ## Root cause `SetupSentry()` adds its own `unhandledrejection` listener, but Sentry's default `GlobalHandlers` integration already captures every unhandled rejection (`onunhandledrejection: true`; we don't override `integrations`). That means every rejection gets reported twice: - **Sentry's built-in copy** handles cross-realm Errors properly via `isError`, and turns Event/CustomEvent rejections into `Event 'CustomEvent' (type=…) captured as promise rejection`. It also puts the type, target and detail into `extra.__serialized__`. - **CE's copy** checks `reason instanceof Error`, and that check fails for Errors created in another realm, such as Safari extension content scripts. It then runs `JSON.stringify(reason)` on them, and since Error properties are non-enumerable the result is `{}`. Evidence from Sentry: - EHY's events line up timestamp for timestamp with properly titled built-in events in other issues. One example is [COMPILER-EXPLORER-DJ2](https://compiler-explorer.sentry.io/issues/COMPILER-EXPLORER-DJ2): `Invalid call to runtime.sendMessage(). Tab not found.` from a Safari extension, with mechanism `auto.browser.global_handlers.onunhandledrejection`. - The "Request timeout …" variants of EHY match their built-in twins count for count. - The title of the issue #8173 set out to fix (DXM, `Event 'CustomEvent' (type=unhandledrejection) captured as promise rejection`) is itself the built-in handler's format. So the built-in handler was already reporting those events. ## Fix Remove the custom listener, together with the `isEventLike` and `formatEventRejection` helpers that only it used. After this, each rejection is reported once, by Sentry's handler, which gives the more informative result in every case: | rejection reason | built-in handler | removed handler | |---|---|---| | cross-realm `Error` | real type + message + stack | `Non-Error rejection: {}` | | `undefined` / primitive | `Non-Error promise rejection captured with value: undefined` | `Non-Error rejection: undefined` (no stack) | | `CustomEvent` | `Event 'CustomEvent' (type=…)` + serialized type/target/detail | `Event rejection: type=…` | Once this ships, the EHY and EJ9 groups will stop receiving events. The real underlying errors (DJ2 and similar) keep their own issues, where they can be triaged or ignored on their merits. ## Test plan - [x] `npm run ts-check`, `npm run lint`, `npm run test-min` - [ ] After deploy: EHY and EJ9 stop receiving events, and the built-in twins (e.g. DJ2) keep reporting at their usual rate. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
Fixes #8172
Fixes COMPILER-EXPLORER-DXM
Problem
Sentry issue COMPILER-EXPLORER-DXM has accumulated 7,018+ occurrences with essentially no diagnostic information:
CustomEvent: Event 'CustomEvent' (type=unhandledrejection) captured as promise rejectionRoot Cause
Safari sometimes rejects promises with
CustomEvent/Eventobjects instead of Error objects. Ourunhandledrejectionhandler instatic/sentry.ts:110-122converts non-Error rejections to Error objects using:When
JSON.stringify()is called on Event objects, it returns{}because Event properties are non-enumerable. This loses all valuable diagnostic information like:event.type- the event typeevent.target- what triggered the eventevent.detail- custom data for CustomEventsEvidence
Solution
Extract meaningful properties from Event/CustomEvent objects before stringifying:
isEventLike()to detect Event/CustomEvent objectsformatEventRejection()to gettype,target,detailObject.assign()for type-safe property additionBefore
After
Impact
This will allow us to:
Test Plan
Code Review Notes
The fix operates at the right layer - transforming Events into meaningful Error messages at the point of rejection, before Sentry sees them. This is different from Sentry's
ExtraErrorDataintegration which operates on already-serialized errors.🤖 Generated with Claude Code