Skip to content

Commit 91f0724

Browse files
committed
extract out instrumentation handlers into their own methods:
1 parent 537db38 commit 91f0724

File tree

1 file changed

+89
-99
lines changed

1 file changed

+89
-99
lines changed

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 89 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -61,121 +61,111 @@ export class GlobalHandlers implements Integration {
6161
const client = hub.getClient();
6262
const attachStacktrace = client && client.getOptions().attachStacktrace;
6363

64-
if (this._options.onerror) {
64+
if (this._options.onerror && !this._onErrorHandlerInstalled) {
6565
globalHandlerLog('onerror');
66-
this._installGlobalOnErrorHandler(hub, attachStacktrace);
66+
_installGlobalOnErrorHandler(hub, attachStacktrace);
67+
this._onErrorHandlerInstalled = true;
6768
}
6869

69-
if (this._options.onunhandledrejection) {
70+
if (this._options.onunhandledrejection && !this._onUnhandledRejectionHandlerInstalled) {
7071
globalHandlerLog('onunhandledrejection');
71-
this._installGlobalOnUnhandledRejectionHandler(hub, attachStacktrace);
72+
_installGlobalOnUnhandledRejectionHandler(hub, attachStacktrace);
73+
this._onUnhandledRejectionHandlerInstalled = true;
7274
}
7375
}
76+
}
7477

75-
/** JSDoc */
76-
private _installGlobalOnErrorHandler(hub: Hub, attachStacktrace: boolean | undefined): void {
77-
if (this._onErrorHandlerInstalled) {
78-
return;
79-
}
80-
81-
addInstrumentationHandler({
82-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
83-
callback: (data: { msg: any; url: any; line: any; column: any; error: any }) => {
84-
const error = data.error;
85-
const isFailedOwnDelivery = error && error.__sentry_own_request__ === true;
86-
87-
if (shouldIgnoreOnError() || isFailedOwnDelivery) {
88-
return;
89-
}
90-
91-
const event =
92-
error === undefined && isString(data.msg)
93-
? _eventFromIncompleteOnError(data.msg, data.url, data.line, data.column)
94-
: _enhanceEventWithInitialFrame(
95-
eventFromUnknownInput(error || data.msg, undefined, {
96-
attachStacktrace,
97-
rejection: false,
98-
}),
99-
data.url,
100-
data.line,
101-
data.column,
102-
);
103-
104-
addExceptionMechanism(event, {
105-
handled: false,
106-
type: 'onerror',
107-
});
108-
109-
hub.captureEvent(event, {
110-
originalException: error,
111-
});
112-
},
113-
type: 'error',
114-
});
115-
116-
this._onErrorHandlerInstalled = true;
117-
}
118-
119-
/** JSDoc */
120-
private _installGlobalOnUnhandledRejectionHandler(hub: Hub, attachStacktrace: boolean | undefined): void {
121-
if (this._onUnhandledRejectionHandlerInstalled) {
122-
return;
123-
}
78+
/** JSDoc */
79+
function _installGlobalOnErrorHandler(hub: Hub, attachStacktrace: boolean | undefined): void {
80+
addInstrumentationHandler({
81+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
82+
callback: (data: { msg: any; url: any; line: any; column: any; error: any }) => {
83+
const error = data.error;
84+
const isFailedOwnDelivery = error && error.__sentry_own_request__ === true;
85+
86+
if (shouldIgnoreOnError() || isFailedOwnDelivery) {
87+
return;
88+
}
89+
90+
const event =
91+
error === undefined && isString(data.msg)
92+
? _eventFromIncompleteOnError(data.msg, data.url, data.line, data.column)
93+
: _enhanceEventWithInitialFrame(
94+
eventFromUnknownInput(error || data.msg, undefined, {
95+
attachStacktrace,
96+
rejection: false,
97+
}),
98+
data.url,
99+
data.line,
100+
data.column,
101+
);
102+
103+
addExceptionMechanism(event, {
104+
handled: false,
105+
type: 'onerror',
106+
});
107+
108+
hub.captureEvent(event, {
109+
originalException: error,
110+
});
111+
},
112+
type: 'error',
113+
});
114+
}
124115

125-
addInstrumentationHandler({
126-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
127-
callback: (e: any) => {
128-
let error = e;
129-
130-
// dig the object of the rejection out of known event types
131-
try {
132-
// PromiseRejectionEvents store the object of the rejection under 'reason'
133-
// see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
134-
if ('reason' in e) {
135-
error = e.reason;
136-
}
137-
// something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents
138-
// to CustomEvents, moving the `promise` and `reason` attributes of the PRE into
139-
// the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec
140-
// see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and
141-
// https://github.com/getsentry/sentry-javascript/issues/2380
142-
else if ('detail' in e && 'reason' in e.detail) {
143-
error = e.detail.reason;
144-
}
145-
} catch (_oO) {
146-
// no-empty
116+
/** JSDoc */
117+
function _installGlobalOnUnhandledRejectionHandler(hub: Hub, attachStacktrace: boolean | undefined): void {
118+
addInstrumentationHandler({
119+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
120+
callback: (e: any) => {
121+
let error = e;
122+
123+
// dig the object of the rejection out of known event types
124+
try {
125+
// PromiseRejectionEvents store the object of the rejection under 'reason'
126+
// see https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
127+
if ('reason' in e) {
128+
error = e.reason;
147129
}
148-
149-
const isFailedOwnDelivery = error && error.__sentry_own_request__ === true;
150-
if (shouldIgnoreOnError() || isFailedOwnDelivery) {
151-
return true;
130+
// something, somewhere, (likely a browser extension) effectively casts PromiseRejectionEvents
131+
// to CustomEvents, moving the `promise` and `reason` attributes of the PRE into
132+
// the CustomEvent's `detail` attribute, since they're not part of CustomEvent's spec
133+
// see https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent and
134+
// https://github.com/getsentry/sentry-javascript/issues/2380
135+
else if ('detail' in e && 'reason' in e.detail) {
136+
error = e.detail.reason;
152137
}
138+
} catch (_oO) {
139+
// no-empty
140+
}
153141

154-
const event = isPrimitive(error)
155-
? _eventFromRejectionWithPrimitive(error)
156-
: eventFromUnknownInput(error, undefined, {
157-
attachStacktrace,
158-
rejection: true,
159-
});
142+
const isFailedOwnDelivery = error && error.__sentry_own_request__ === true;
143+
if (shouldIgnoreOnError() || isFailedOwnDelivery) {
144+
return true;
145+
}
160146

161-
event.level = Severity.Error;
147+
const event = isPrimitive(error)
148+
? _eventFromRejectionWithPrimitive(error)
149+
: eventFromUnknownInput(error, undefined, {
150+
attachStacktrace,
151+
rejection: true,
152+
});
162153

163-
addExceptionMechanism(event, {
164-
handled: false,
165-
type: 'onunhandledrejection',
166-
});
154+
event.level = Severity.Error;
167155

168-
hub.captureEvent(event, {
169-
originalException: error,
170-
});
156+
addExceptionMechanism(event, {
157+
handled: false,
158+
type: 'onunhandledrejection',
159+
});
171160

172-
return;
173-
},
174-
type: 'unhandledrejection',
175-
});
161+
hub.captureEvent(event, {
162+
originalException: error,
163+
});
176164

177-
this._onUnhandledRejectionHandlerInstalled = true;
178-
}
165+
return;
166+
},
167+
type: 'unhandledrejection',
168+
});
179169
}
180170

181171
/**

0 commit comments

Comments
 (0)