Skip to content

Commit bd94e8d

Browse files
authored
ref(core): Streamline global handlers & add comments (#14568)
This adds a comment explaining why we are using `window.onerror` instead of `window.addEventListener('error')`, for future reference. It also removes the check for `__SENTRY_LOADER__` check, which we removed quite some time ago in the Loader.
1 parent d631127 commit bd94e8d

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

packages/core/src/utils-hoist/instrument/globalError.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export function addGlobalErrorInstrumentationHandler(handler: (data: HandlerData
2020
function instrumentError(): void {
2121
_oldOnErrorHandler = GLOBAL_OBJ.onerror;
2222

23+
// Note: The reason we are doing window.onerror instead of window.addEventListener('error') is
24+
// is that we are using this handler in the Loader Script, to handle buffered errors consistently
2325
GLOBAL_OBJ.onerror = function (
2426
msg: string | object,
2527
url?: string,
@@ -36,7 +38,7 @@ function instrumentError(): void {
3638
};
3739
triggerHandlers('error', handlerData);
3840

39-
if (_oldOnErrorHandler && !_oldOnErrorHandler.__SENTRY_LOADER__) {
41+
if (_oldOnErrorHandler) {
4042
// eslint-disable-next-line prefer-rest-params
4143
return _oldOnErrorHandler.apply(this, arguments);
4244
}

packages/core/src/utils-hoist/instrument/globalUnhandledRejection.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
31
import type { HandlerDataUnhandledRejection } from '../../types-hoist';
42

53
import { GLOBAL_OBJ } from '../worldwide';
@@ -24,11 +22,13 @@ export function addGlobalUnhandledRejectionInstrumentationHandler(
2422
function instrumentUnhandledRejection(): void {
2523
_oldOnUnhandledRejectionHandler = GLOBAL_OBJ.onunhandledrejection;
2624

27-
GLOBAL_OBJ.onunhandledrejection = function (e: any): boolean {
25+
// Note: The reason we are doing window.onerror instead of window.addEventListener('unhandledrejection') is
26+
// is that we are using this handler in the Loader Script, to handle buffered rejections consistently
27+
GLOBAL_OBJ.onunhandledrejection = function (e: unknown): boolean {
2828
const handlerData: HandlerDataUnhandledRejection = e;
2929
triggerHandlers('unhandledrejection', handlerData);
3030

31-
if (_oldOnUnhandledRejectionHandler && !_oldOnUnhandledRejectionHandler.__SENTRY_LOADER__) {
31+
if (_oldOnUnhandledRejectionHandler) {
3232
// eslint-disable-next-line prefer-rest-params
3333
return _oldOnUnhandledRejectionHandler.apply(this, arguments);
3434
}

packages/core/src/utils-hoist/worldwide.ts

-2
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ export type InternalGlobal = {
5656
onerror?: {
5757
(event: object | string, source?: string, lineno?: number, colno?: number, error?: Error): any;
5858
__SENTRY_INSTRUMENTED__?: true;
59-
__SENTRY_LOADER__?: true;
6059
};
6160
onunhandledrejection?: {
6261
(event: unknown): boolean;
6362
__SENTRY_INSTRUMENTED__?: true;
64-
__SENTRY_LOADER__?: true;
6563
};
6664
SENTRY_ENVIRONMENT?: string;
6765
SENTRY_DSN?: string;

0 commit comments

Comments
 (0)