Skip to content

Commit 2620be8

Browse files
committed
fix(node-core): do not print rejection warning in strict mode
This matches the behavior of Node.js.
1 parent d75440c commit 2620be8

5 files changed

Lines changed: 60 additions & 8 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const Sentry = require('@sentry/node-core');
2+
const { setupOtel } = require('../../../utils/setupOtel.js');
3+
const { expectProcessToExit } = require('../../../utils/expect-process-to-exit');
4+
5+
const client = Sentry.init({
6+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
7+
integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'strict' })],
8+
});
9+
10+
setupOtel(client);
11+
12+
expectProcessToExit();
13+
14+
Promise.reject(new Error('test rejection'));

dev-packages/node-core-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,23 @@ Error: test rejection
5252
expect(err?.code).toBe(1);
5353
expect(stdout).not.toBe("I'm alive!");
5454
expect(stderr)
55-
.toContain(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
56-
test rejection`);
55+
.toBe('test rejection\n');
56+
done();
57+
});
58+
}));
59+
60+
test('should show error-type promise rejections in strict mode', () =>
61+
new Promise<void>(done => {
62+
expect.assertions(4);
63+
64+
const testScriptPath = path.resolve(__dirname, 'mode-strict-error.js');
65+
66+
childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => {
67+
expect(err).not.toBeNull();
68+
expect(err?.code).toBe(1);
69+
expect(stdout).not.toBe("I'm alive!");
70+
expect(stderr)
71+
.toContain('Error: test rejection');
5772
done();
5873
});
5974
}));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Sentry = require('@sentry/node');
2+
const { expectProcessToExit } = require('../../../utils/expect-process-to-exit');
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'strict' })],
7+
});
8+
9+
expectProcessToExit();
10+
11+
Promise.reject(new Error('test rejection'));

dev-packages/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,23 @@ Error: test rejection
5353
expect(err?.code).toBe(1);
5454
expect(stdout).not.toBe("I'm alive!");
5555
expect(stderr)
56-
.toContain(`This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
57-
test rejection`);
56+
.toBe('test rejection\n');
57+
done();
58+
});
59+
}));
60+
61+
test('should show error-type promise rejections in strict mode', () =>
62+
new Promise<void>(done => {
63+
expect.assertions(4);
64+
65+
const testScriptPath = path.resolve(__dirname, 'mode-strict-error.js');
66+
67+
childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => {
68+
expect(err).not.toBeNull();
69+
expect(err?.code).toBe(1);
70+
expect(stdout).not.toBe("I'm alive!");
71+
expect(stderr)
72+
.toContain('Error: test rejection');
5873
done();
5974
});
6075
}));

packages/node-core/src/integrations/onunhandledrejection.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,9 @@ function handleRejection(reason: unknown, mode: UnhandledRejectionMode): void {
140140
if (mode === 'warn') {
141141
consoleSandbox(() => {
142142
console.warn(rejectionWarning);
143-
console.error(reason && typeof reason === 'object' && 'stack' in reason ? reason.stack : reason);
143+
console.error(reason);
144144
});
145145
} else if (mode === 'strict') {
146-
consoleSandbox(() => {
147-
console.warn(rejectionWarning);
148-
});
149146
logAndExitProcess(reason);
150147
}
151148
/* eslint-enable no-console */

0 commit comments

Comments
 (0)