Skip to content

Commit

Permalink
fix(node): Log entire error object in OnUncaughtException (#8876)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Lms24 authored Aug 28, 2023
1 parent 4be150e commit 5afb861
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Sentry = require('@sentry/node');

Sentry.init({
dsn: 'https://[email protected]/1337',
});

throw new Error('foo', { cause: 'bar' });
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ describe('OnUncaughtException integration', () => {
});
});

test('should log entire error object to console stderr', done => {
expect.assertions(2);

const testScriptPath = path.resolve(__dirname, 'log-entire-error-to-console.js');

childProcess.exec(`node ${testScriptPath}`, { encoding: 'utf8' }, (err, stderr) => {
expect(err).not.toBeNull();
const errString = err?.toString() || '';

expect(errString).toContain(stderr);

done();
});
});

describe('with `exitEvenIfOtherHandlersAreRegistered` set to false', () => {
test('should close process on uncaught error with no additional listeners registered', done => {
expect.assertions(3);
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/integrations/utils/errorhandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeClient>();

Expand Down

0 comments on commit 5afb861

Please sign in to comment.