Extract diagnostic info from Safari CustomEvent rejections

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 <noreply@anthropic.com>
This commit is contained in:
Matt Godbolt
2025-10-07 15:04:44 -05:00
co-authored by Claude
parent 6e87d56844
commit d491362f29
+29 -7
View File
@@ -55,6 +55,21 @@ export function setSentryLayout(l: GoldenLayout) {
});
}
function isEventLike(value: unknown): value is Event {
return value instanceof Event || value?.constructor?.name === 'Event' || value?.constructor?.name === 'CustomEvent';
}
function formatEventRejection(evt: Event): string {
const targetName = evt.target?.constructor?.name ?? 'unknown';
let message = `Event rejection: type="${evt.type}", target="${targetName}"`;
if ('detail' in evt && evt.detail !== undefined) {
message += `, detail=${JSON.stringify(evt.detail)}`;
}
return message;
}
export function SetupSentry() {
if (options.statusTrackingEnabled && options.sentryDsn) {
Sentry.init({
@@ -108,16 +123,23 @@ export function SetupSentry() {
},
});
window.addEventListener('unhandledrejection', event => {
// Convert non-Error rejection reasons to Error objects
let reason = event.reason;
if (!(reason instanceof Error)) {
const errorMessage =
typeof reason === 'string' ? reason : `Non-Error rejection: ${JSON.stringify(reason)}`;
reason = new Error(errorMessage);
// Preserve original reason for debugging
(reason as any).originalReason = event.reason;
if (!(reason instanceof Error)) {
// Safari sometimes rejects promises with CustomEvent/Event objects.
// Extract useful properties instead of stringifying to empty object.
// See: https://github.com/compiler-explorer/compiler-explorer/issues/8172
// Related: https://github.com/getsentry/sentry-javascript/issues/2210
const errorMessage =
typeof reason === 'string'
? reason
: isEventLike(reason)
? formatEventRejection(reason)
: `Non-Error rejection: ${JSON.stringify(reason)}`;
reason = Object.assign(new Error(errorMessage), {originalReason: event.reason});
}
SentryCapture(reason, 'Unhandled Promise Rejection');
});
}