Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ab5e94c

Browse filesBrowse files
ljharbtargos
authored andcommitted
util: display a present-but-undefined error cause
See #41097 (comment) PR-URL: #41247 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 24fbbf2 commit ab5e94c
Copy full SHA for ab5e94c

File tree

Expand file treeCollapse file tree

4 files changed

+21
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+21
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+1-1Lines changed: 1 addition & 1 deletion
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
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/message/util-inspect-error-cause.js
+4Lines changed: 4 additions & 0 deletions
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 });
Collapse file

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

Copy file name to clipboardExpand all lines: test/message/util-inspect-error-cause.out
+10Lines changed: 10 additions & 0 deletions
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 {
Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+6Lines changed: 6 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.