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

Better support for Error.cause in util.inspect/console.log #40859

Closed
targos opened this issue Nov 18, 2021 · 3 comments · Fixed by #41002
Closed

Better support for Error.cause in util.inspect/console.log #40859

targos opened this issue Nov 18, 2021 · 3 comments · Fixed by #41002
Assignees
Labels
errors Issues and PRs related to JavaScript errors originated in Node.js core. feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.

Comments

@targos
Copy link
Member

targos commented Nov 18, 2021

Is your feature request related to a problem? Please describe.

Given this code:

const one = new Error('one');
const two = new Error('two', { cause: one });
const three = new Error('three', { cause: two });
console.log(three);
Error: three
    at Object.<anonymous> (/test.js:3:15)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
    ...

The output only contains the error message and stack for error three.
It is already possible to see the value of cause, when showHidden is enabled, but in my opinion this is not a good general solution because it makes the output too verbose and requires to set a global config for console.log.

Describe the solution you'd like

I'm open to suggestions. One idea I have is to output something recursively indented like this:

Error: three
    at Object.<anonymous> (/test.js:3:15)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
    ...
Caused by:
  Error: two
      at Object.<anonymous> (/test.js:2:13)
      at Module._compile (node:internal/modules/cjs/loader:1095:14)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
      ...
  Caused by:
    Error: one
        at Object.<anonymous> (/test.js:1:13)
        at Module._compile (node:internal/modules/cjs/loader:1095:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
        ...

/cc @nodejs/util

@targos targos added feature request Issues that request new features to be added to Node.js. errors Issues and PRs related to JavaScript errors originated in Node.js core. labels Nov 18, 2021
@Mesteery Mesteery added the util Issues and PRs related to the built-in util module. label Nov 18, 2021
@BridgeAR BridgeAR self-assigned this Nov 18, 2021
@BridgeAR
Copy link
Member

I implemented something locally. It always prints the non-enumerable cause property and hides duplicated stack frames as long as they are included in the causing error stack as well. @targos does this match your needs?

image

@targos
Copy link
Member Author

targos commented Nov 25, 2021

Looks good to me!

@kanongil
Copy link
Contributor

There is already a bit of discussion on this here: #38725 (comment)

nodejs-github-bot pushed a commit that referenced this issue Dec 2, 2021
While inspecting errors, always visualize the cause. That property
is non-enumerable by default while being useful in general for
debugging.

Duplicated stack frames are hidden.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: #41002
Fixes: #40859
Fixes: #38725
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
danielleadams pushed a commit that referenced this issue Dec 14, 2021
While inspecting errors, always visualize the cause. That property
is non-enumerable by default while being useful in general for
debugging.

Duplicated stack frames are hidden.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: #41002
Fixes: #40859
Fixes: #38725
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
danielleadams pushed a commit that referenced this issue Jan 31, 2022
While inspecting errors, always visualize the cause. That property
is non-enumerable by default while being useful in general for
debugging.

Duplicated stack frames are hidden.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: #41002
Fixes: #40859
Fixes: #38725
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
danielleadams pushed a commit that referenced this issue Jan 31, 2022
While inspecting errors, always visualize the cause. That property
is non-enumerable by default while being useful in general for
debugging.

Duplicated stack frames are hidden.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: #41002
Fixes: #40859
Fixes: #38725
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Linkgoron pushed a commit to Linkgoron/node that referenced this issue Jan 31, 2022
While inspecting errors, always visualize the cause. That property
is non-enumerable by default while being useful in general for
debugging.

Duplicated stack frames are hidden.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: nodejs#41002
Fixes: nodejs#40859
Fixes: nodejs#38725
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
danielleadams pushed a commit that referenced this issue Feb 1, 2022
While inspecting errors, always visualize the cause. That property
is non-enumerable by default while being useful in general for
debugging.

Duplicated stack frames are hidden.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: #41002
Fixes: #40859
Fixes: #38725
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
errors Issues and PRs related to JavaScript errors originated in Node.js core. feature request Issues that request new features to be added to Node.js. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants