Skip to content

Commit 4bbb553

Browse files
authored
fix(node): Set handled to false when it is a crash (#3493)
Set mechanism handled to false when error is triggered by an onuncaughtexception or onunhandledrejection. Also, add the appropriate exception type to mechanism.
1 parent 0cc0f4b commit 4bbb553

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

packages/node/src/backend.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export class NodeBackend extends BaseBackend<NodeOptions> {
5353
public eventFromException(exception: any, hint?: EventHint): PromiseLike<Event> {
5454
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5555
let ex: any = exception;
56-
const mechanism: Mechanism = {
56+
const providedMechanism: Mechanism | undefined =
57+
hint && hint.data && (hint.data as { mechanism: Mechanism }).mechanism;
58+
const mechanism: Mechanism = providedMechanism || {
5759
handled: true,
5860
type: 'generic',
5961
};

packages/node/src/integrations/onuncaughtexception.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ export class OnUncaughtException implements Integration {
7878
if (hub.getIntegration(OnUncaughtException)) {
7979
hub.withScope((scope: Scope) => {
8080
scope.setLevel(Severity.Fatal);
81-
hub.captureException(error, { originalException: error });
81+
hub.captureException(error, {
82+
originalException: error,
83+
data: { mechanism: { handled: false, type: 'onuncaughtexception' } },
84+
});
8285
if (!calledFatalError) {
8386
calledFatalError = true;
8487
onFatalError(error);

packages/node/src/integrations/onunhandledrejection.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ export class OnUnhandledRejection implements Integration {
6969
scope.setExtras(context.extra);
7070
}
7171

72-
hub.captureException(reason, { originalException: promise });
72+
hub.captureException(reason, {
73+
originalException: promise,
74+
data: { mechanism: { handled: false, type: 'onunhandledrejection' } },
75+
});
7376
});
7477
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
7578

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Hub } from '@sentry/hub';
2+
3+
import { OnUncaughtException } from '../src/integrations/onuncaughtexception';
4+
5+
jest.mock('@sentry/hub', () => {
6+
// we just want to short-circuit it, so dont worry about types
7+
const original = jest.requireActual('@sentry/hub');
8+
original.Hub.prototype.getIntegration = () => true;
9+
return {
10+
...original,
11+
getCurrentHub: () => new Hub(),
12+
};
13+
});
14+
15+
describe('uncaught exceptions', () => {
16+
test('install global listener', () => {
17+
const integration = new OnUncaughtException();
18+
integration.setupOnce();
19+
expect(process.listeners('uncaughtException')).toHaveLength(1);
20+
});
21+
22+
test('sendUncaughtException', () => {
23+
const integration = new OnUncaughtException({ onFatalError: jest.fn() });
24+
integration.setupOnce();
25+
26+
const captureException = jest.spyOn(Hub.prototype, 'captureException');
27+
28+
integration.handler({ message: 'message', name: 'name' });
29+
30+
expect(captureException.mock.calls[0][1]?.data).toEqual({
31+
mechanism: { handled: false, type: 'onuncaughtexception' },
32+
});
33+
});
34+
});

packages/node/test/onunhandledrejection.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ describe('unhandled promises', () => {
4242

4343
integration.sendUnhandledPromise('bla', promise);
4444

45+
expect(captureException.mock.calls[0][1]?.data).toEqual({
46+
mechanism: { handled: false, type: 'onunhandledrejection' },
47+
});
4548
expect(captureException.mock.calls[0][0]).toBe('bla');
4649
expect(setUser.mock.calls[0][0]).toEqual({ id: 1 });
4750
expect(setExtra.mock.calls[0]).toEqual(['unhandledPromiseRejection', true]);

0 commit comments

Comments
 (0)