Skip to content

Commit 5afb861

Browse files
authored
fix(node): Log entire error object in OnUncaughtException (#8876)
In our `OnUncaughtException` integration, we have to emulate Node's default behaviour of logging errors to the console. For some reason though, we only logged the stack trace of an `error` with a stack trace instead of the entire error object. As reported in #8856, this causes additional properties on the error (such as `cause`) not to be logged anymore which doesn't reflect Node's default behaviour (see issue for comparison). This patch simplifies the `console.error` call to just always log the entire `error`.
1 parent 4be150e commit 5afb861

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const Sentry = require('@sentry/node');
2+
3+
Sentry.init({
4+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
5+
});
6+
7+
throw new Error('foo', { cause: 'bar' });

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ describe('OnUncaughtException integration', () => {
2828
});
2929
});
3030

31+
test('should log entire error object to console stderr', done => {
32+
expect.assertions(2);
33+
34+
const testScriptPath = path.resolve(__dirname, 'log-entire-error-to-console.js');
35+
36+
childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stderr) => {
37+
expect(err).not.toBeNull();
38+
const errString = err?.toString() || '';
39+
40+
expect(errString).toContain(stderr);
41+
42+
done();
43+
});
44+
});
45+
3146
describe('with `exitEvenIfOtherHandlersAreRegistered` set to false', () => {
3247
test('should close process on uncaught error with no additional listeners registered', done => {
3348
expect.assertions(3);

packages/node/src/integrations/utils/errorhandling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const DEFAULT_SHUTDOWN_TIMEOUT = 2000;
1010
*/
1111
export function logAndExitProcess(error: Error): void {
1212
// eslint-disable-next-line no-console
13-
console.error(error && error.stack ? error.stack : error);
13+
console.error(error);
1414

1515
const client = getCurrentHub().getClient<NodeClient>();
1616

0 commit comments

Comments
 (0)