Skip to content

Commit

Permalink
util: serialize falsy cause values while inspecting errors
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: #41097
Fixes: #41096
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 89d9556 commit 2602c0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,8 @@ function formatError(err, constructor, tag, ctx, keys) {

removeDuplicateErrorKeys(ctx, keys, err, stack);

if (err.cause && (keys.length === 0 || !keys.includes('cause'))) {
if (err.cause !== undefined &&
(keys.length === 0 || !keys.includes('cause'))) {
keys.push('cause');
}

Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,16 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
assert(ex.includes('[message]'));
}

{
const falsyCause1 = new Error('', { cause: false });
delete falsyCause1.stack;
const falsyCause2 = new Error(undefined, { cause: null });
falsyCause2.stack = '';

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

{
const tmp = Error.stackTraceLimit;
Error.stackTraceLimit = 0;
Expand Down

0 comments on commit 2602c0c

Please sign in to comment.