Skip to content

Commit 387d3a7

Browse files
committed
fix unhandled rejection for null
1 parent b03f591 commit 387d3a7

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
import { getCurrentHub } from '@sentry/core';
3-
import type {
4-
Event,
5-
EventHint,
6-
HandlerDataUnhandledRejection,
7-
Hub,
8-
Integration,
9-
Primitive,
10-
StackParser,
11-
} from '@sentry/types';
3+
import type { Event, EventHint, Hub, Integration, Primitive, StackParser } from '@sentry/types';
124
import {
135
addExceptionMechanism,
146
addGlobalErrorInstrumentationHandler,
@@ -85,7 +77,6 @@ export class GlobalHandlers implements Integration {
8577
}
8678
}
8779

88-
/** JSDoc */
8980
function _installGlobalOnErrorHandler(): void {
9081
addGlobalErrorInstrumentationHandler(data => {
9182
const [hub, stackParser, attachStacktrace] = getHubAndOptions();
@@ -113,38 +104,19 @@ function _installGlobalOnErrorHandler(): void {
113104
});
114105
}
115106

116-
/** JSDoc */
117107
function _installGlobalOnUnhandledRejectionHandler(): void {
118108
addGlobalUnhandledRejectionInstrumentationHandler(e => {
119109
const [hub, stackParser, attachStacktrace] = getHubAndOptions();
120110
if (!hub.getIntegration(GlobalHandlers)) {
121111
return;
122112
}
123-
let error: Error | HandlerDataUnhandledRejection = e;
124-
125-
// dig the object of the rejection out of known event types
126-
try {
127-
// PromiseRejectionEvents store the object of the rejection under 'reason'
128-
// see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
129-
if ('reason' in e && e.reason) {
130-
error = e.reason;
131-
}
132-
// something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents
133-
// to CustomEvents, moving the `promise` and `reason` attributes of the PRE into
134-
// the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec
135-
// see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and
136-
// https://github.com/getsentry/sentry-javascript/issues/2380
137-
else if ('detail' in e && e.detail && 'reason' in e.detail && e.detail.reason) {
138-
error = e.detail.reason;
139-
}
140-
} catch (_oO) {
141-
// no-empty
142-
}
143113

144114
if (shouldIgnoreOnError()) {
145115
return true;
146116
}
147117

118+
const error = _getUnhandledRejectionError(e as unknown);
119+
148120
const event = isPrimitive(error)
149121
? _eventFromRejectionWithPrimitive(error)
150122
: eventFromUnknownInput(stackParser, error, undefined, attachStacktrace, true);
@@ -156,6 +128,35 @@ function _installGlobalOnUnhandledRejectionHandler(): void {
156128
});
157129
}
158130

131+
function _getUnhandledRejectionError(error: unknown): unknown {
132+
if (isPrimitive(error)) {
133+
return error;
134+
}
135+
136+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
137+
const e = error as any;
138+
139+
// dig the object of the rejection out of known event types
140+
try {
141+
// PromiseRejectionEvents store the object of the rejection under 'reason'
142+
// see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
143+
if ('reason' in e) {
144+
return e.reason;
145+
}
146+
147+
// something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents
148+
// to CustomEvents, moving the `promise` and `reason` attributes of the PRE into
149+
// the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec
150+
// see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and
151+
// https://github.com/getsentry/sentry-javascript/issues/2380
152+
else if ('detail' in e && 'reason' in e.detail) {
153+
return e.detail.reason;
154+
}
155+
} catch {} // eslint-disable-line no-empty
156+
157+
return error;
158+
}
159+
159160
/**
160161
* Create an event from a promise rejection where the `reason` is a primitive.
161162
*

packages/types/src/instrument.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,4 @@ export interface HandlerDataError {
8585
url?: string;
8686
}
8787

88-
export interface HandlerDataUnhandledRejection {
89-
reason?: Error;
90-
detail?: { reason?: Error };
91-
}
88+
export type HandlerDataUnhandledRejection = unknown;

0 commit comments

Comments
 (0)