Better support for Error.cause in util.inspect/console.log #40859
Copy link
Copy link
Closed
Labels
errorsIssues and PRs related to JavaScript errors originating in Node.js core.Issues and PRs related to JavaScript errors originating in Node.js core.feature requestIssues requesting new Node.js features.Issues requesting new Node.js features.utilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
errorsIssues and PRs related to JavaScript errors originating in Node.js core.Issues and PRs related to JavaScript errors originating in Node.js core.feature requestIssues requesting new Node.js features.Issues requesting new Node.js features.utilIssues and PRs related to the built-in util module.Issues and PRs related to the built-in util module.
Activity
BridgeAR commentedon Nov 24, 2021
I implemented something locally. It always prints the non-enumerable
causeproperty and hides duplicated stack frames as long as they are included in the causing error stack as well. @targos does this match your needs?targos commentedon Nov 25, 2021
Looks good to me!
kanongil commentedon Nov 28, 2021
There is already a bit of discussion on this here: #38725 (comment)
util: always visualize cause property in errors during inspection
util: always visualize cause property in errors during inspection
util: always visualize cause property in errors during inspection
util: always visualize cause property in errors during inspection
util: always visualize cause property in errors during inspection