Skip to content

Commit ab5e94c

Browse files
ljharbtargos
authored andcommitted
util: display a present-but-undefined error cause
See #41097 (comment) PR-URL: #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]>
1 parent 24fbbf2 commit ab5e94c

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

lib/internal/util/inspect.js

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

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

1285-
if (err.cause !== undefined &&
1285+
if ('cause' in err &&
12861286
(keys.length === 0 || !keys.includes('cause'))) {
12871287
keys.push('cause');
12881288
}

test/message/util-inspect-error-cause.js

+4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ const cause5 = new Error('Object cause', {
2222
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
2323
}
2424
});
25+
const cause6 = new Error('undefined cause', {
26+
cause: undefined
27+
});
2528

2629
console.log(cause4);
2730
console.log(cause5);
31+
console.log(cause6);
2832

2933
process.nextTick(() => {
3034
const error = new RangeError('New Stack Frames', { cause: cause2 });

test/message/util-inspect-error-cause.out

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ Error: Object cause
2323
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
2424
}
2525
}
26+
Error: undefined cause
27+
at *
28+
at *
29+
at *
30+
at *
31+
at *
32+
at *
33+
at * {
34+
[cause]: undefined
35+
}
2636
RangeError: New Stack Frames
2737
at *
2838
*[90m at *[39m {

test/parallel/test-util-inspect.js

+6
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,15 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
665665
delete falsyCause1.stack;
666666
const falsyCause2 = new Error(undefined, { cause: null });
667667
falsyCause2.stack = '';
668+
const undefinedCause = new Error('', { cause: undefined });
669+
undefinedCause.stack = '';
668670

669671
assert.strictEqual(util.inspect(falsyCause1), '[Error] { [cause]: false }');
670672
assert.strictEqual(util.inspect(falsyCause2), '[Error] { [cause]: null }');
673+
assert.strictEqual(
674+
util.inspect(undefinedCause),
675+
'[Error] { [cause]: undefined }'
676+
);
671677
}
672678

673679
{

0 commit comments

Comments
 (0)