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: nodejs#41096

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
BridgeAR committed Dec 6, 2021
1 parent ca353de commit 91d904d
Show file tree
Hide file tree
Showing 3 changed files with 38 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 @@ -1198,7 +1198,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
12 changes: 12 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,18 @@ const cause2 = new FoobarError('Individual message', { cause: cause1 });
cause2.extraProperties = 'Yes!';
const cause3 = new Error('Stack causes', { cause: cause2 });

const cause4 = new Error('No 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: No 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 91d904d

Please sign in to comment.