Skip to content

Commit 66b5083

Browse files
BridgeARdanielleadams
authored andcommitted
util: serialize falsy cause values while inspecting errors
Signed-off-by: Ruben Bridgewater <[email protected]> PR-URL: #41097 Fixes: #41096 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Anto Aravinth <[email protected]>
1 parent 09d29ca commit 66b5083

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/internal/util/inspect.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,8 @@ function formatError(err, constructor, tag, ctx, keys) {
12821282

12831283
removeDuplicateErrorKeys(ctx, keys, err, stack);
12841284

1285-
if (err.cause && (keys.length === 0 || !keys.includes('cause'))) {
1285+
if (err.cause !== undefined &&
1286+
(keys.length === 0 || !keys.includes('cause'))) {
12861287
keys.push('cause');
12871288
}
12881289

test/parallel/test-util-inspect.js

+10
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,16 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
660660
assert(ex.includes('[message]'));
661661
}
662662

663+
{
664+
const falsyCause1 = new Error('', { cause: false });
665+
delete falsyCause1.stack;
666+
const falsyCause2 = new Error(undefined, { cause: null });
667+
falsyCause2.stack = '';
668+
669+
assert.strictEqual(util.inspect(falsyCause1), '[Error] { [cause]: false }');
670+
assert.strictEqual(util.inspect(falsyCause2), '[Error] { [cause]: null }');
671+
}
672+
663673
{
664674
const tmp = Error.stackTraceLimit;
665675
Error.stackTraceLimit = 0;

0 commit comments

Comments
 (0)