Skip to content

Commit 09d29ca

Browse files
BridgeARdanielleadams
authored andcommitted
util: make sure error causes of any type may be inspected
An error cause may be of any type. Handle all of them, no matter if they are an error or not. Fixes: #41096 Signed-off-by: Ruben Bridgewater <[email protected]> PR-URL: #41097 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 f5ff88b commit 09d29ca

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

lib/internal/util/inspect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ function getStackFrames(ctx, err, stack) {
12151215
const frames = stack.split('\n');
12161216

12171217
// Remove stack frames identical to frames in cause.
1218-
if (err.cause) {
1218+
if (err.cause && isError(err.cause)) {
12191219
const causeStack = getStackString(err.cause);
12201220
const causeStackStart = causeStack.indexOf('\n at');
12211221
if (causeStackStart !== -1) {

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

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ const cause2 = new FoobarError('Individual message', { cause: cause1 });
1313
cause2.extraProperties = 'Yes!';
1414
const cause3 = new Error('Stack causes', { cause: cause2 });
1515

16+
const cause4 = new Error('Number error cause', { cause: 42 });
17+
const cause5 = new Error('Object cause', {
18+
cause: {
19+
message: 'Unique',
20+
name: 'Error',
21+
stack: 'Error: Unique\n' +
22+
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
23+
}
24+
});
25+
26+
console.log(cause4);
27+
console.log(cause5);
28+
1629
process.nextTick(() => {
1730
const error = new RangeError('New Stack Frames', { cause: cause2 });
1831
const error2 = new RangeError('New Stack Frames', { cause: cause3 });

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

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
Error: Number error cause
2+
at *
3+
at *
4+
at *
5+
at *
6+
at *
7+
at *
8+
at * {
9+
[cause]: 42
10+
}
11+
Error: Object cause
12+
at *
13+
at *
14+
at *
15+
at *
16+
at *
17+
at *
18+
at * {
19+
[cause]: {
20+
message: 'Unique',
21+
name: 'Error',
22+
stack: 'Error: Unique\n' +
23+
' at Module._compile (node:internal/modules/cjs/loader:827:30)'
24+
}
25+
}
126
RangeError: New Stack Frames
227
at *
328
*[90m at *[39m {

0 commit comments

Comments
 (0)