diff --git a/dev-packages/node-core-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-strict-error.js b/dev-packages/node-core-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-strict-error.js new file mode 100644 index 000000000000..1f87de5ebff9 --- /dev/null +++ b/dev-packages/node-core-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-strict-error.js @@ -0,0 +1,14 @@ +const Sentry = require('@sentry/node-core'); +const { setupOtel } = require('../../../utils/setupOtel.js'); +const { expectProcessToExit } = require('../../../utils/expect-process-to-exit'); + +const client = Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'strict' })], +}); + +setupOtel(client); + +expectProcessToExit(); + +Promise.reject(new Error('test rejection')); diff --git a/dev-packages/node-core-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts b/dev-packages/node-core-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts index 8ee873a4f017..7a020f162a8d 100644 --- a/dev-packages/node-core-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts +++ b/dev-packages/node-core-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts @@ -58,6 +58,22 @@ test rejection`); }); })); + test('should not show warning for error-type promise rejections in strict mode', () => + new Promise(done => { + expect.assertions(5); + + const testScriptPath = path.resolve(__dirname, 'mode-strict-error.js'); + + childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => { + expect(err).not.toBeNull(); + expect(err?.code).toBe(1); + expect(stdout).not.toBe("I'm alive!"); + expect(stderr).toContain('Error: test rejection'); + expect(stderr).not.toContain('The promise rejected with the reason'); + done(); + }); + })); + test('should not close process or warn on unhandled rejection in none mode', () => new Promise(done => { expect.assertions(3); diff --git a/dev-packages/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-strict-error.js b/dev-packages/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-strict-error.js new file mode 100644 index 000000000000..7204bb8482a1 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/mode-strict-error.js @@ -0,0 +1,11 @@ +const Sentry = require('@sentry/node'); +const { expectProcessToExit } = require('../../../utils/expect-process-to-exit'); + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + integrations: [Sentry.onUnhandledRejectionIntegration({ mode: 'strict' })], +}); + +expectProcessToExit(); + +Promise.reject(new Error('test rejection')); diff --git a/dev-packages/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts b/dev-packages/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts index c8570747cf8d..2b90612ed505 100644 --- a/dev-packages/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts +++ b/dev-packages/node-integration-tests/suites/public-api/onUnhandledRejectionIntegration/test.ts @@ -59,6 +59,22 @@ test rejection`); }); })); + test('should not show warning for error-type promise rejections in strict mode', () => + new Promise(done => { + expect.assertions(5); + + const testScriptPath = path.resolve(__dirname, 'mode-strict-error.js'); + + childProcess.execFile('node', [testScriptPath], { encoding: 'utf8' }, (err, stdout, stderr) => { + expect(err).not.toBeNull(); + expect(err?.code).toBe(1); + expect(stdout).not.toBe("I'm alive!"); + expect(stderr).toContain('Error: test rejection'); + expect(stderr).not.toContain('The promise rejected with the reason'); + done(); + }); + })); + test('should not close process or warn on unhandled rejection in none mode', () => new Promise(done => { expect.assertions(3); diff --git a/packages/node-core/src/integrations/onunhandledrejection.ts b/packages/node-core/src/integrations/onunhandledrejection.ts index 8e2483d6a8cb..434534e9a036 100644 --- a/packages/node-core/src/integrations/onunhandledrejection.ts +++ b/packages/node-core/src/integrations/onunhandledrejection.ts @@ -143,9 +143,11 @@ function handleRejection(reason: unknown, mode: UnhandledRejectionMode): void { console.error(reason && typeof reason === 'object' && 'stack' in reason ? reason.stack : reason); }); } else if (mode === 'strict') { - consoleSandbox(() => { - console.warn(rejectionWarning); - }); + if (!(reason && typeof reason === 'object' && 'stack' in reason)) { + consoleSandbox(() => { + console.warn(rejectionWarning); + }); + } logAndExitProcess(reason); } /* eslint-enable no-console */