diff --git a/CHANGELOG.md b/CHANGELOG.md index 105423ec1afc..67bb4c714686 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,9 @@ - [browser] fix: Make `addBreadcrumb` sync internally, `beforeBreadcrumb` is now only sync - [browser] fix: Remove internal `console` guard in `beforeBreadcrumb` -- [core]: Integrations now live on the `Client`. This means that when binding a new Client to the `Hub` the client +- [core] feat: Integrations now live on the `Client`. This means that when binding a new Client to the `Hub` the client itself can decide which integration should run. +- [node] ref: Simplify Node global handlers code ## 4.1.1 diff --git a/packages/node/src/integrations/onuncaughtexception.ts b/packages/node/src/integrations/onuncaughtexception.ts index f7f875c1ec8f..282058e1ce21 100644 --- a/packages/node/src/integrations/onuncaughtexception.ts +++ b/packages/node/src/integrations/onuncaughtexception.ts @@ -1,5 +1,5 @@ import { getCurrentHub, Scope } from '@sentry/core'; -import { Integration, SentryEvent, Severity } from '@sentry/types'; +import { Integration, Severity } from '@sentry/types'; import { logger } from '@sentry/utils/logger'; import { defaultOnFatalError } from '../handlers'; @@ -33,7 +33,7 @@ export class OnUncaughtException implements Integration { * @inheritDoc */ public setupOnce(): void { - global.process.on('uncaughtException', this.handler); + global.process.on('uncaughtException', this.handler.bind(this)); } } @@ -49,23 +49,18 @@ export function makeErrorHandler( return (error: Error): void => { if (!caughtFirstError) { + const hub = getCurrentHub(); + // this is the first uncaught error and the ultimate reason for shutting down // we want to do absolutely everything possible to ensure it gets captured // also we want to make sure we don't go recursion crazy if more errors happen after this one firstError = error; caughtFirstError = true; - if (getCurrentHub().getIntegration(OnUncaughtException)) { - getCurrentHub().withScope(async () => { - getCurrentHub().configureScope((scope: Scope) => { - scope.addEventProcessor(async (event: SentryEvent) => ({ - ...event, - level: Severity.Fatal, - })); - }); - - getCurrentHub().captureException(error, { originalException: error }); - + if (hub.getIntegration(OnUncaughtException)) { + hub.withScope(async (scope: Scope) => { + scope.setLevel(Severity.Fatal); + hub.captureException(error, { originalException: error }); if (!calledFatalError) { calledFatalError = true; onFatalError(error); diff --git a/packages/node/src/integrations/onunhandledrejection.ts b/packages/node/src/integrations/onunhandledrejection.ts index 99728aaefe9e..a227fcb23bfa 100644 --- a/packages/node/src/integrations/onunhandledrejection.ts +++ b/packages/node/src/integrations/onunhandledrejection.ts @@ -1,4 +1,4 @@ -import { getCurrentHub } from '@sentry/core'; +import { getCurrentHub, Scope } from '@sentry/core'; import { Integration } from '@sentry/types'; /** Global Promise Rejection handler */ @@ -25,29 +25,33 @@ export class OnUnhandledRejection implements Integration { * @param promise promise */ public sendUnhandledPromise(reason: any, promise: any): void { - if (!getCurrentHub().getIntegration(OnUnhandledRejection)) { + const hub = getCurrentHub(); + + if (!hub.getIntegration(OnUnhandledRejection)) { return; } + const context = (promise.domain && promise.domain.sentryContext) || {}; - getCurrentHub().withScope(() => { - getCurrentHub().configureScope(scope => { - // Preserve backwards compatibility with raven-node for now - if (context.user) { - scope.setUser(context.user); - } - if (context.tags) { - Object.keys(context.tags).forEach(key => { - scope.setTag(key, context.tags[key]); - }); - } - if (context.extra) { - Object.keys(context.extra).forEach(key => { - scope.setExtra(key, context.extra[key]); - }); - } - scope.setExtra('unhandledPromiseRejection', true); - }); - getCurrentHub().captureException(reason, { originalException: promise }); + + hub.withScope((scope: Scope) => { + scope.setExtra('unhandledPromiseRejection', true); + + // Preserve backwards compatibility with raven-node for now + if (context.user) { + scope.setUser(context.user); + } + if (context.tags) { + Object.keys(context.tags).forEach(key => { + scope.setTag(key, context.tags[key]); + }); + } + if (context.extra) { + Object.keys(context.extra).forEach(key => { + scope.setExtra(key, context.extra[key]); + }); + } + + hub.captureException(reason, { originalException: promise }); }); } } diff --git a/packages/node/test/onunhandledrejection.test.ts b/packages/node/test/onunhandledrejection.test.ts index 4f95b2ad2293..84208b1b90d2 100644 --- a/packages/node/test/onunhandledrejection.test.ts +++ b/packages/node/test/onunhandledrejection.test.ts @@ -29,7 +29,8 @@ describe('unhandled promises', () => { expect(captureException.mock.calls[0][0]).toBe('bla'); expect(setUser.mock.calls[0][0]).toEqual({ id: 1 }); - expect(setExtra.mock.calls[0]).toEqual(['extra', '1']); + expect(setExtra.mock.calls[0]).toEqual(['unhandledPromiseRejection', true]); + expect(setExtra.mock.calls[1]).toEqual(['extra', '1']); expect(setTag.mock.calls[0]).toEqual(['tag', '2']); }); });