Skip to content

Commit 75ff820

Browse files
committed
fixup! util: always visualize cause property in errors during inspection
Signed-off-by: Ruben Bridgewater <[email protected]>
1 parent a795424 commit 75ff820

File tree

3 files changed

+102
-20
lines changed

3 files changed

+102
-20
lines changed

lib/internal/util/inspect.js

+21-17
Original file line numberDiff line numberDiff line change
@@ -1274,27 +1274,31 @@ function formatError(err, constructor, tag, ctx, keys) {
12741274
const stackStart = stack.indexOf('\n at', pos);
12751275
if (stackStart === -1) {
12761276
stack = `[${stack}]`;
1277-
} else if (ctx.colors) {
1278-
// Highlight userland code and node modules.
1277+
} else {
12791278
let newStack = stack.slice(0, stackStart);
12801279
const lines = getStackFrames(ctx, err, stack.slice(stackStart + 1));
1281-
for (const line of lines) {
1282-
const core = line.match(coreModuleRegExp);
1283-
if (core !== null && NativeModule.exists(core[1])) {
1284-
newStack += `\n${ctx.stylize(line, 'undefined')}`;
1285-
} else {
1286-
// This adds underscores to all node_modules to quickly identify them.
1287-
let nodeModule;
1288-
newStack += '\n';
1289-
let pos = 0;
1290-
while (nodeModule = nodeModulesRegExp.exec(line)) {
1291-
// '/node_modules/'.length === 14
1292-
newStack += line.slice(pos, nodeModule.index + 14);
1293-
newStack += ctx.stylize(nodeModule[1], 'module');
1294-
pos = nodeModule.index + nodeModule[0].length;
1280+
if (ctx.colors) {
1281+
// Highlight userland code and node modules.
1282+
for (const line of lines) {
1283+
const core = line.match(coreModuleRegExp);
1284+
if (core !== null && NativeModule.exists(core[1])) {
1285+
newStack += `\n${ctx.stylize(line, 'undefined')}`;
1286+
} else {
1287+
// This adds underscores to all node_modules to quickly identify them.
1288+
let nodeModule;
1289+
newStack += '\n';
1290+
let pos = 0;
1291+
while (nodeModule = nodeModulesRegExp.exec(line)) {
1292+
// '/node_modules/'.length === 14
1293+
newStack += line.slice(pos, nodeModule.index + 14);
1294+
newStack += ctx.stylize(nodeModule[1], 'module');
1295+
pos = nodeModule.index + nodeModule[0].length;
1296+
}
1297+
newStack += pos === 0 ? line : line.slice(pos);
12951298
}
1296-
newStack += pos === 0 ? line : line.slice(pos);
12971299
}
1300+
} else {
1301+
newStack += `\n${lines.join('\n')}`;
12981302
}
12991303
stack = newStack;
13001304
}

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require('../common');
44

5+
const { inspect } = require('util');
6+
57
class FoobarError extends Error {
68
status = 'Feeling good';
79
}
@@ -15,7 +17,15 @@ process.nextTick(() => {
1517
const error = new RangeError('New Stack Frames', { cause: cause2 });
1618
const error2 = new RangeError('New Stack Frames', { cause: cause3 });
1719

18-
console.log(error);
19-
console.log(cause3);
20-
console.log(error2);
20+
inspect.defaultOptions.colors = true;
21+
22+
console.log(inspect(error));
23+
console.log(inspect(cause3));
24+
console.log(inspect(error2));
25+
26+
inspect.defaultOptions.colors = false;
27+
28+
console.log(inspect(error));
29+
console.log(inspect(cause3));
30+
console.log(inspect(error2));
2131
});

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

+68
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,71 @@
1+
RangeError: New Stack Frames
2+
at *
3+
*[90m at *[39m {
4+
[cause]: FoobarError: Individual message
5+
at *
6+
*[90m at *[39m
7+
*[90m ... 4 lines matching cause stack trace ...*[39m
8+
*[90m at *[39m {
9+
status: *[32m'Feeling good'*[39m,
10+
extraProperties: *[32m'Yes!'*[39m,
11+
[cause]: TypeError: Inner error
12+
at *
13+
*[90m at *[39m
14+
*[90m at *[39m
15+
*[90m at *[39m
16+
*[90m at *[39m
17+
*[90m at *[39m
18+
*[90m at *[39m
19+
}
20+
}
21+
Error: Stack causes
22+
at *
23+
*[90m at *[39m
24+
*[90m ... 4 lines matching cause stack trace ...*[39m
25+
*[90m at *[39m {
26+
[cause]: FoobarError: Individual message
27+
at *
28+
*[90m at *[39m
29+
*[90m ... 4 lines matching cause stack trace ...*[39m
30+
*[90m at *[39m {
31+
status: *[32m'Feeling good'*[39m,
32+
extraProperties: *[32m'Yes!'*[39m,
33+
[cause]: TypeError: Inner error
34+
at *
35+
*[90m at *[39m
36+
*[90m at *[39m
37+
*[90m at *[39m
38+
*[90m at *[39m
39+
*[90m at *[39m
40+
*[90m at *[39m
41+
}
42+
}
43+
RangeError: New Stack Frames
44+
at *
45+
*[90m at *[39m {
46+
[cause]: Error: Stack causes
47+
at *
48+
*[90m at *[39m
49+
*[90m ... 4 lines matching cause stack trace ...*[39m
50+
*[90m at *[39m {
51+
[cause]: FoobarError: Individual message
52+
at *
53+
*[90m at *[39m
54+
*[90m ... 4 lines matching cause stack trace ...*[39m
55+
*[90m at *[39m {
56+
status: *[32m'Feeling good'*[39m,
57+
extraProperties: *[32m'Yes!'*[39m,
58+
[cause]: TypeError: Inner error
59+
at *
60+
*[90m at *[39m
61+
*[90m at *[39m
62+
*[90m at *[39m
63+
*[90m at *[39m
64+
*[90m at *[39m
65+
*[90m at *[39m
66+
}
67+
}
68+
}
169
RangeError: New Stack Frames
270
at *
371
at * {

0 commit comments

Comments
 (0)