Skip to content

Commit 793b104

Browse files
committed
dynamic dispatch :)
1 parent 41deb4a commit 793b104

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import {
1313
import { eventFromUnknownInput } from '../eventbuilder';
1414
import { shouldIgnoreOnError } from '../helpers';
1515

16+
type GlobalHandlersIntegrationsOptionKeys = 'onerror' | 'onunhandledrejection';
17+
1618
/** JSDoc */
17-
interface GlobalHandlersIntegrations {
18-
onerror: boolean;
19-
onunhandledrejection: boolean;
20-
}
19+
type GlobalHandlersIntegrations = Record<GlobalHandlersIntegrationsOptionKeys, boolean>;
20+
21+
type InstallFunc = (hub: Hub, attachStacktrace: boolean | undefined) => void;
2122

2223
/** Global handlers */
2324
export class GlobalHandlers implements Integration {
@@ -34,11 +35,14 @@ export class GlobalHandlers implements Integration {
3435
/** JSDoc */
3536
private readonly _options: GlobalHandlersIntegrations;
3637

37-
/** JSDoc */
38-
private _onErrorHandlerInstalled: boolean = false;
39-
40-
/** JSDoc */
41-
private _onUnhandledRejectionHandlerInstalled: boolean = false;
38+
/**
39+
* Stores references functions to installing handlers. Will set to undefined
40+
* after they have been run so that they are not used twice.
41+
*/
42+
private _installFunc: Record<GlobalHandlersIntegrationsOptionKeys, InstallFunc | undefined> = {
43+
onerror: _installGlobalOnErrorHandler,
44+
onunhandledrejection: _installGlobalOnUnhandledRejectionHandler,
45+
};
4246

4347
/** JSDoc */
4448
public constructor(options?: GlobalHandlersIntegrations) {
@@ -57,17 +61,18 @@ export class GlobalHandlers implements Integration {
5761
const hub = getCurrentHub();
5862
const client = hub.getClient();
5963
const attachStacktrace = client && client.getOptions().attachStacktrace;
60-
61-
if (this._options.onerror || !this._onErrorHandlerInstalled) {
62-
globalHandlerLog('onerror');
63-
_installGlobalOnErrorHandler(hub, attachStacktrace);
64-
this._onErrorHandlerInstalled = true;
65-
}
66-
67-
if (this._options.onunhandledrejection || !this._onUnhandledRejectionHandlerInstalled) {
68-
globalHandlerLog('onunhandledrejection');
69-
_installGlobalOnUnhandledRejectionHandler(hub, attachStacktrace);
70-
this._onUnhandledRejectionHandlerInstalled = true;
64+
const options = this._options;
65+
66+
// We can disable guard-for-in as we construct the options object above + do checks against
67+
// `this._installFunc` for the property.
68+
// eslint-disable-next-line guard-for-in
69+
for (const key in options) {
70+
const installFunc = this._installFunc[key as GlobalHandlersIntegrationsOptionKeys];
71+
if (installFunc && options[key as GlobalHandlersIntegrationsOptionKeys]) {
72+
globalHandlerLog(key);
73+
installFunc(hub, attachStacktrace);
74+
this._installFunc[key as GlobalHandlersIntegrationsOptionKeys] = undefined;
75+
}
7176
}
7277
}
7378
}
@@ -81,9 +86,7 @@ function _installGlobalOnErrorHandler(hub: Hub, attachStacktrace: boolean | unde
8186
return;
8287
}
8388
const { msg, url, line, column, error } = data;
84-
const isFailedOwnDelivery = error && error.__sentry_own_request__ === true;
85-
86-
if (shouldIgnoreOnError() || isFailedOwnDelivery) {
89+
if (shouldIgnoreOnError() || (error && error.__sentry_own_request__)) {
8790
return;
8891
}
8992

@@ -135,8 +138,7 @@ function _installGlobalOnUnhandledRejectionHandler(hub: Hub, attachStacktrace: b
135138
// no-empty
136139
}
137140

138-
const isFailedOwnDelivery = error && error.__sentry_own_request__ === true;
139-
if (shouldIgnoreOnError() || isFailedOwnDelivery) {
141+
if (shouldIgnoreOnError() || (error && error.__sentry_own_request__)) {
140142
return true;
141143
}
142144

0 commit comments

Comments
 (0)