Skip to content

Commit b7cf873

Browse files
authored
ref: Simplify Node global handlers code (#1662)
1 parent 81a3412 commit b7cf873

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
- [browser] fix: Make `addBreadcrumb` sync internally, `beforeBreadcrumb` is now only sync
66
- [browser] fix: Remove internal `console` guard in `beforeBreadcrumb`
7-
- [core]: Integrations now live on the `Client`. This means that when binding a new Client to the `Hub` the client
7+
- [core] feat: Integrations now live on the `Client`. This means that when binding a new Client to the `Hub` the client
88
itself can decide which integration should run.
9+
- [node] ref: Simplify Node global handlers code
910

1011
## 4.1.1
1112

packages/node/src/integrations/onuncaughtexception.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentHub, Scope } from '@sentry/core';
2-
import { Integration, SentryEvent, Severity } from '@sentry/types';
2+
import { Integration, Severity } from '@sentry/types';
33
import { logger } from '@sentry/utils/logger';
44
import { defaultOnFatalError } from '../handlers';
55

@@ -33,7 +33,7 @@ export class OnUncaughtException implements Integration {
3333
* @inheritDoc
3434
*/
3535
public setupOnce(): void {
36-
global.process.on('uncaughtException', this.handler);
36+
global.process.on('uncaughtException', this.handler.bind(this));
3737
}
3838
}
3939

@@ -49,23 +49,18 @@ export function makeErrorHandler(
4949

5050
return (error: Error): void => {
5151
if (!caughtFirstError) {
52+
const hub = getCurrentHub();
53+
5254
// this is the first uncaught error and the ultimate reason for shutting down
5355
// we want to do absolutely everything possible to ensure it gets captured
5456
// also we want to make sure we don't go recursion crazy if more errors happen after this one
5557
firstError = error;
5658
caughtFirstError = true;
5759

58-
if (getCurrentHub().getIntegration(OnUncaughtException)) {
59-
getCurrentHub().withScope(async () => {
60-
getCurrentHub().configureScope((scope: Scope) => {
61-
scope.addEventProcessor(async (event: SentryEvent) => ({
62-
...event,
63-
level: Severity.Fatal,
64-
}));
65-
});
66-
67-
getCurrentHub().captureException(error, { originalException: error });
68-
60+
if (hub.getIntegration(OnUncaughtException)) {
61+
hub.withScope(async (scope: Scope) => {
62+
scope.setLevel(Severity.Fatal);
63+
hub.captureException(error, { originalException: error });
6964
if (!calledFatalError) {
7065
calledFatalError = true;
7166
onFatalError(error);
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentHub } from '@sentry/core';
1+
import { getCurrentHub, Scope } from '@sentry/core';
22
import { Integration } from '@sentry/types';
33

44
/** Global Promise Rejection handler */
@@ -25,29 +25,33 @@ export class OnUnhandledRejection implements Integration {
2525
* @param promise promise
2626
*/
2727
public sendUnhandledPromise(reason: any, promise: any): void {
28-
if (!getCurrentHub().getIntegration(OnUnhandledRejection)) {
28+
const hub = getCurrentHub();
29+
30+
if (!hub.getIntegration(OnUnhandledRejection)) {
2931
return;
3032
}
33+
3134
const context = (promise.domain && promise.domain.sentryContext) || {};
32-
getCurrentHub().withScope(() => {
33-
getCurrentHub().configureScope(scope => {
34-
// Preserve backwards compatibility with raven-node for now
35-
if (context.user) {
36-
scope.setUser(context.user);
37-
}
38-
if (context.tags) {
39-
Object.keys(context.tags).forEach(key => {
40-
scope.setTag(key, context.tags[key]);
41-
});
42-
}
43-
if (context.extra) {
44-
Object.keys(context.extra).forEach(key => {
45-
scope.setExtra(key, context.extra[key]);
46-
});
47-
}
48-
scope.setExtra('unhandledPromiseRejection', true);
49-
});
50-
getCurrentHub().captureException(reason, { originalException: promise });
35+
36+
hub.withScope((scope: Scope) => {
37+
scope.setExtra('unhandledPromiseRejection', true);
38+
39+
// Preserve backwards compatibility with raven-node for now
40+
if (context.user) {
41+
scope.setUser(context.user);
42+
}
43+
if (context.tags) {
44+
Object.keys(context.tags).forEach(key => {
45+
scope.setTag(key, context.tags[key]);
46+
});
47+
}
48+
if (context.extra) {
49+
Object.keys(context.extra).forEach(key => {
50+
scope.setExtra(key, context.extra[key]);
51+
});
52+
}
53+
54+
hub.captureException(reason, { originalException: promise });
5155
});
5256
}
5357
}

packages/node/test/onunhandledrejection.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ describe('unhandled promises', () => {
2929

3030
expect(captureException.mock.calls[0][0]).toBe('bla');
3131
expect(setUser.mock.calls[0][0]).toEqual({ id: 1 });
32-
expect(setExtra.mock.calls[0]).toEqual(['extra', '1']);
32+
expect(setExtra.mock.calls[0]).toEqual(['unhandledPromiseRejection', true]);
33+
expect(setExtra.mock.calls[1]).toEqual(['extra', '1']);
3334
expect(setTag.mock.calls[0]).toEqual(['tag', '2']);
3435
});
3536
});

0 commit comments

Comments
 (0)