Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printed errors should show cause #8856

Closed
3 tasks done
OliverJAsh opened this issue Aug 22, 2023 · 2 comments · Fixed by #8876
Closed
3 tasks done

Printed errors should show cause #8856

OliverJAsh opened this issue Aug 22, 2023 · 2 comments · Fixed by #8876
Assignees
Labels
Package: node Issues related to the Sentry Node SDK Type: Bug

Comments

@OliverJAsh
Copy link
Contributor

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

7.64.0

Framework Version

No response

Link to Sentry event

No response

SDK Setup

No response

Steps to Reproduce

const Sentry = require('@sentry/node');
Sentry.init({ dsn: 'https://[email protected]/123' });

throw new Error('foo', { cause: 'bar' });

Expected Result

The printed error should include the cause.

Actual Result

The printed error does not include the cause:

$ node test.js
Error: foo
    at Object.<anonymous> (/Users/oliver/Code/unsplash/unsplash-web/test.js:4:7)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

If we remove Sentry.init, the cause is shown:

$ node test.js
/Users/oliver/Code/unsplash/unsplash-web/test.js:4
throw new Error('foo', { cause: 'bar' });
^

Error: foo
    at Object.<anonymous> (/Users/oliver/Code/unsplash/unsplash-web/test.js:4:7)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  [cause]: 'bar'
}

It seems that Sentry is changing the way that exceptions are printed. I believe this custom logic needs to be updated to include the cause. Furthermore, I am wondering if it's possible to disable this and use Node's default behaviour?

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Aug 22, 2023
@github-actions github-actions bot added the Package: node Issues related to the Sentry Node SDK label Aug 22, 2023
@Lms24 Lms24 self-assigned this Aug 28, 2023
@Lms24
Copy link
Member

Lms24 commented Aug 28, 2023

Hi @OliverJAsh thanks for writing in!

I took a look at this and can confirm that we're not showing the cause (or fwiw any other properties of an error), in case the error has a stack trace.

First of all, the Node SDK registers a handler for UncaughtException which will override the default behaviour. By default, the entire error object seems to be logged, along with the code line and the Node version (basically, what you showed in your last snippet).

We try to emulate this default behaviour by explicitly logging the error to the console but for some reason, we only log the stack trace if the error has one (as in your example), meaning, we omit other error properties, such as cause:

console.error(error && error.stack ? error.stack : error);

I tried tracking down why we're doing this but couldn't find an explanation other than that we seemed to have added it when we moved from raven-node (very old SDK version) to @sentry/node.

I'll open a PR to just always log the entire error instead here. This won't 1:1 replicate the entire default logging behaviour but at least it'll show you cause (or other properties).

@Lms24
Copy link
Member

Lms24 commented Aug 28, 2023

FYI, the cause field is only shown on Node 16 and newer. Previous Node versions only printed the stack trace of the error when calling console.error(err) but also when directly throwing the error and using Node's default behaviour.

Lms24 added a commit that referenced this issue Aug 28, 2023
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: node Issues related to the Sentry Node SDK Type: Bug
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants