Skip to content

Commit

Permalink
util: make sure error causes of any type may be inspected
Browse files Browse the repository at this point in the history
An error cause may be of any type. Handle all of them, no matter
if they are an error or not.

Fixes: #41096

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

PR-URL: #41097
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
  • Loading branch information
BridgeAR authored and danielleadams committed Feb 1, 2022
1 parent 8ce8588 commit 89d9556
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ function getStackFrames(ctx, err, stack) {
const frames = stack.split('\n');

// Remove stack frames identical to frames in cause.
if (err.cause) {
if (err.cause && isError(err.cause)) {
const causeStack = getStackString(err.cause);
const causeStackStart = causeStack.indexOf('\n at');
if (causeStackStart !== -1) {
Expand Down
13 changes: 13 additions & 0 deletions test/message/util-inspect-error-cause.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ const cause2 = new FoobarError('Individual message', { cause: cause1 });
cause2.extraProperties = 'Yes!';
const cause3 = new Error('Stack causes', { cause: cause2 });

const cause4 = new Error('Number error cause', { cause: 42 });
const cause5 = new Error('Object cause', {
cause: {
message: 'Unique',
name: 'Error',
stack: 'Error: Unique\n' +
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
}
});

console.log(cause4);
console.log(cause5);

process.nextTick(() => {
const error = new RangeError('New Stack Frames', { cause: cause2 });
const error2 = new RangeError('New Stack Frames', { cause: cause3 });
Expand Down
25 changes: 25 additions & 0 deletions test/message/util-inspect-error-cause.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
Error: Number error cause
at *
at *
at *
at *
at *
at *
at * {
[cause]: 42
}
Error: Object cause
at *
at *
at *
at *
at *
at *
at * {
[cause]: {
message: 'Unique',
name: 'Error',
stack: 'Error: Unique\n' +
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
}
}
RangeError: New Stack Frames
at *
*[90m at *[39m {
Expand Down

0 comments on commit 89d9556

Please sign in to comment.