Skip to content

Commit

Permalink
util: display a present-but-undefined error cause
Browse files Browse the repository at this point in the history
See nodejs#41097 (comment)

PR-URL: nodejs#41247
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
ljharb authored and Linkgoron committed Jan 31, 2022
1 parent dfe3188 commit f32fd4c
Show file tree
Hide file tree
Showing 4 changed files with 21 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 @@ -1282,7 +1282,7 @@ function formatError(err, constructor, tag, ctx, keys) {

removeDuplicateErrorKeys(ctx, keys, err, stack);

if (err.cause !== undefined &&
if ('cause' in err &&
(keys.length === 0 || !keys.includes('cause'))) {
keys.push('cause');
}
Expand Down
4 changes: 4 additions & 0 deletions test/message/util-inspect-error-cause.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ const cause5 = new Error('Object cause', {
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
}
});
const cause6 = new Error('undefined cause', {
cause: undefined
});

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

process.nextTick(() => {
const error = new RangeError('New Stack Frames', { cause: cause2 });
Expand Down
10 changes: 10 additions & 0 deletions test/message/util-inspect-error-cause.out
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ Error: Object cause
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
}
}
Error: undefined cause
at *
at *
at *
at *
at *
at *
at * {
[cause]: undefined
}
RangeError: New Stack Frames
at *
*[90m at *[39m {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,15 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
delete falsyCause1.stack;
const falsyCause2 = new Error(undefined, { cause: null });
falsyCause2.stack = '';
const undefinedCause = new Error('', { cause: undefined });
undefinedCause.stack = '';

assert.strictEqual(util.inspect(falsyCause1), '[Error] { [cause]: false }');
assert.strictEqual(util.inspect(falsyCause2), '[Error] { [cause]: null }');
assert.strictEqual(
util.inspect(undefinedCause),
'[Error] { [cause]: undefined }'
);
}

{
Expand Down

0 comments on commit f32fd4c

Please sign in to comment.