Skip to content

Commit e352e1e

Browse files
committed
fix(node-core): rejection warning in strict mode
Only print it if the reason doesn't have a stack. This matches the behavior of Node.js.
1 parent d75440c commit e352e1e

5 files changed

Lines changed: 62 additions & 3 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ test rejection`);
5858
});
5959
}));
6060

61+
test('should not show warning for error-type promise rejections in strict mode', () =>
62+
new Promise<void>(done => {
63+
expect.assertions(5);
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).toContain('Error: test rejection');
72+
expect(stderr).not.toContain('The promise rejected with the reason');
73+
done();
74+
});
75+
}));
76+
6177
test('should not close process or warn on unhandled rejection in none mode', () =>
6278
new Promise<void>(done => {
6379
expect.assertions(3);
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ test rejection`);
5959
});
6060
}));
6161

62+
test('should not show warning for error-type promise rejections in strict mode', () =>
63+
new Promise<void>(done => {
64+
expect.assertions(5);
65+
66+
const testScriptPath = path.resolve(__dirname, 'mode-strict-error.js');
67+
68+
childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => {
69+
expect(err).not.toBeNull();
70+
expect(err?.code).toBe(1);
71+
expect(stdout).not.toBe("I'm alive!");
72+
expect(stderr).toContain('Error: test rejection');
73+
expect(stderr).not.toContain('The promise rejected with the reason');
74+
done();
75+
});
76+
}));
77+
6278
test('should not close process or warn on unhandled rejection in none mode', () =>
6379
new Promise<void>(done => {
6480
expect.assertions(3);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ function handleRejection(reason: unknown, mode: UnhandledRejectionMode): void {
143143
console.error(reason && typeof reason === 'object' && 'stack' in reason ? reason.stack : reason);
144144
});
145145
} else if (mode === 'strict') {
146-
consoleSandbox(() => {
147-
console.warn(rejectionWarning);
148-
});
146+
if (!(reason && typeof reason === 'object' && 'stack' in reason)) {
147+
consoleSandbox(() => {
148+
console.warn(rejectionWarning);
149+
});
150+
}
149151
logAndExitProcess(reason);
150152
}
151153
/* eslint-enable no-console */

0 commit comments

Comments
 (0)