diff --git a/tracker/tracker/src/main/modules/exception.ts b/tracker/tracker/src/main/modules/exception.ts index 96e4266c0..8d286349f 100644 --- a/tracker/tracker/src/main/modules/exception.ts +++ b/tracker/tracker/src/main/modules/exception.ts @@ -37,6 +37,7 @@ export function getExceptionMessage(error: Error, fallbackStack: Array): void { }, opts, ) - if (options.captureExceptions) { - const handler = (e: ErrorEvent | PromiseRejectionEvent): void => { - const msg = getExceptionMessageFromEvent(e) + function patchContext(context: Window & typeof globalThis) { + function handler(e: ErrorEvent | PromiseRejectionEvent): void { + const msg = getExceptionMessageFromEvent(e, context) if (msg != null) { app.send(msg) } } - - app.attachEventListener(window, 'unhandledrejection', (e: PromiseRejectionEvent): void => - handler(e), - ) - app.attachEventListener(window, 'error', (e: ErrorEvent): void => handler(e)) + app.attachEventListener(context, 'unhandledrejection', handler) + app.attachEventListener(context, 'error', handler) + } + if (options.captureExceptions) { + app.observer.attachContextCallback(patchContext) + patchContext(window) } }