diff --git a/packages/deno/src/integrations/globalhandlers.ts b/packages/deno/src/integrations/globalhandlers.ts index 9914764d4c45..27745d6d6765 100644 --- a/packages/deno/src/integrations/globalhandlers.ts +++ b/packages/deno/src/integrations/globalhandlers.ts @@ -1,7 +1,8 @@ import type { ServerRuntimeClient } from '@sentry/core'; -import { getClient, getCurrentHub, getCurrentScope } from '@sentry/core'; +import { captureEvent } from '@sentry/core'; +import { getClient } from '@sentry/core'; import { flush } from '@sentry/core'; -import type { Event, Hub, Integration, Primitive, StackParser } from '@sentry/types'; +import type { Client, Event, Integration, Primitive, StackParser } from '@sentry/types'; import { eventFromUnknownInput, isPrimitive } from '@sentry/utils'; type GlobalHandlersIntegrationsOptionKeys = 'error' | 'unhandledrejection'; @@ -26,15 +27,6 @@ export class GlobalHandlers implements Integration { /** JSDoc */ private readonly _options: GlobalHandlersIntegrations; - /** - * Stores references functions to installing handlers. Will set to undefined - * after they have been run so that they are not used twice. - */ - private _installFunc: Record void) | undefined> = { - error: installGlobalErrorHandler, - unhandledrejection: installGlobalUnhandledRejectionHandler, - }; - /** JSDoc */ public constructor(options?: GlobalHandlersIntegrations) { this._options = { @@ -47,35 +39,35 @@ export class GlobalHandlers implements Integration { * @inheritDoc */ public setupOnce(): void { - const options = this._options; - - // We can disable guard-for-in as we construct the options object above + do checks against - // `this._installFunc` for the property. - // eslint-disable-next-line guard-for-in - for (const key in options) { - const installFunc = this._installFunc[key as GlobalHandlersIntegrationsOptionKeys]; - if (installFunc && options[key as GlobalHandlersIntegrationsOptionKeys]) { - installFunc(); - this._installFunc[key as GlobalHandlersIntegrationsOptionKeys] = undefined; - } + // noop + } + + /** @inheritdoc */ + public setup(client: Client): void { + if (this._options.error) { + installGlobalErrorHandler(client); + } + if (this._options.unhandledrejection) { + installGlobalUnhandledRejectionHandler(client); } } } -function installGlobalErrorHandler(): void { +function installGlobalErrorHandler(client: Client): void { globalThis.addEventListener('error', data => { - if (isExiting) { + if (getClient() !== client || isExiting) { return; } - const [hub, stackParser] = getHubAndOptions(); + const stackParser = getStackParser(); + const { message, error } = data; const event = eventFromUnknownInput(getClient(), stackParser, error || message); event.level = 'fatal'; - hub.captureEvent(event, { + captureEvent(event, { originalException: error, mechanism: { handled: false, @@ -94,13 +86,13 @@ function installGlobalErrorHandler(): void { }); } -function installGlobalUnhandledRejectionHandler(): void { +function installGlobalUnhandledRejectionHandler(client: Client): void { globalThis.addEventListener('unhandledrejection', (e: PromiseRejectionEvent) => { - if (isExiting) { + if (getClient() !== client || isExiting) { return; } - const [hub, stackParser] = getHubAndOptions(); + const stackParser = getStackParser(); let error = e; // dig the object of the rejection out of known event types @@ -118,7 +110,7 @@ function installGlobalUnhandledRejectionHandler(): void { event.level = 'fatal'; - hub.captureEvent(event, { + captureEvent(event, { originalException: error, mechanism: { handled: false, @@ -157,12 +149,12 @@ function eventFromRejectionWithPrimitive(reason: Primitive): Event { }; } -function getHubAndOptions(): [Hub, StackParser] { - const hub = getCurrentHub(); - const client = hub.getClient(); - const options = (client && client.getOptions()) || { - stackParser: () => [], - attachStacktrace: false, - }; - return [hub, options.stackParser]; +function getStackParser(): StackParser { + const client = getClient(); + + if (!client) { + return () => []; + } + + return client.getOptions().stackParser; }