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 Jan 31, 2022
1 parent 5b9e46b commit fc00dc0
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
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
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
@@ -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 fc00dc0

Please sign in to comment.