From 3fd431fb4ffb2e33355dbfa77c7e8d6fc667fe1f Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 28 Aug 2023 11:26:25 +0200 Subject: [PATCH 1/3] fix(node): Log entire error object in OnUncaughtException --- .../log-entire-error-to-console.js | 7 +++++++ .../suites/public-api/OnUncaughtException/test.ts | 12 ++++++++++++ .../node/src/integrations/utils/errorhandling.ts | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 packages/node-integration-tests/suites/public-api/OnUncaughtException/log-entire-error-to-console.js diff --git a/packages/node-integration-tests/suites/public-api/OnUncaughtException/log-entire-error-to-console.js b/packages/node-integration-tests/suites/public-api/OnUncaughtException/log-entire-error-to-console.js new file mode 100644 index 000000000000..758f9e26cc5b --- /dev/null +++ b/packages/node-integration-tests/suites/public-api/OnUncaughtException/log-entire-error-to-console.js @@ -0,0 +1,7 @@ +const Sentry = require('@sentry/node'); + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', +}); + +throw new Error('foo', { cause: 'bar' }); diff --git a/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts b/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts index 00c8459466c9..d017e42bb756 100644 --- a/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts +++ b/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts @@ -28,6 +28,18 @@ describe('OnUncaughtException integration', () => { }); }); + test('should log entire error object to console stderr', done => { + expect.assertions(1); + + const testScriptPath = path.resolve(__dirname, 'log-entire-error-to-console.js'); + + childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => { + expect(stderr).toEqual(expect.stringMatching(/Error: foo(\n.*)+ \[cause\]: 'bar'/gm)); + + done(); + }); + }); + describe('with `exitEvenIfOtherHandlersAreRegistered` set to false', () => { test('should close process on uncaught error with no additional listeners registered', done => { expect.assertions(3); diff --git a/packages/node/src/integrations/utils/errorhandling.ts b/packages/node/src/integrations/utils/errorhandling.ts index e4c7a14924a1..cf52929fa642 100644 --- a/packages/node/src/integrations/utils/errorhandling.ts +++ b/packages/node/src/integrations/utils/errorhandling.ts @@ -10,7 +10,7 @@ const DEFAULT_SHUTDOWN_TIMEOUT = 2000; */ export function logAndExitProcess(error: Error): void { // eslint-disable-next-line no-console - console.error(error && error.stack ? error.stack : error); + console.error(error); const client = getCurrentHub().getClient(); From d37dcce7743ecc300db7193e21f49621a12e9b35 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 28 Aug 2023 12:33:15 +0200 Subject: [PATCH 2/3] adjust tests for node versions --- .../suites/public-api/OnUncaughtException/test.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts b/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts index d017e42bb756..84ead4e7bd9b 100644 --- a/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts +++ b/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts @@ -29,12 +29,21 @@ describe('OnUncaughtException integration', () => { }); test('should log entire error object to console stderr', done => { - expect.assertions(1); + const nodeVersion = Number(process.version.replace('v', '').split('.')[0]); + expect.assertions(nodeVersion >= 16 ? 3 : 2); const testScriptPath = path.resolve(__dirname, 'log-entire-error-to-console.js'); - childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stdout, stderr) => { - expect(stderr).toEqual(expect.stringMatching(/Error: foo(\n.*)+ \[cause\]: 'bar'/gm)); + childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stderr) => { + expect(err).not.toBeNull(); + const errString = err?.toString() || ''; + + expect(errString).toContain(stderr); + + if (nodeVersion >= 16) { + // additional error properties are only printed to console since Node 16 :( + expect(stderr).toContain("[cause]: 'bar'"); + } done(); }); From 6ca5f8a135d506aa75c858af5f688a1744f18b53 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 28 Aug 2023 12:51:28 +0200 Subject: [PATCH 3/3] simplify test --- .../suites/public-api/OnUncaughtException/test.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts b/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts index 84ead4e7bd9b..c90dd989e37f 100644 --- a/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts +++ b/packages/node-integration-tests/suites/public-api/OnUncaughtException/test.ts @@ -29,8 +29,7 @@ describe('OnUncaughtException integration', () => { }); test('should log entire error object to console stderr', done => { - const nodeVersion = Number(process.version.replace('v', '').split('.')[0]); - expect.assertions(nodeVersion >= 16 ? 3 : 2); + expect.assertions(2); const testScriptPath = path.resolve(__dirname, 'log-entire-error-to-console.js'); @@ -40,11 +39,6 @@ describe('OnUncaughtException integration', () => { expect(errString).toContain(stderr); - if (nodeVersion >= 16) { - // additional error properties are only printed to console since Node 16 :( - expect(stderr).toContain("[cause]: 'bar'"); - } - done(); }); });