Skip to content

Commit f0d6a37

Browse files
BridgeARMylesBorins
authored andcommitted
util: fix inspected stack indentation
Error stacks and multiline error messages were not correct indented. This is fixed by this patch. PR-URL: #20802 Refs: #20253 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 38bc5fb commit f0d6a37

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

lib/util.js

+5
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,11 @@ function formatValue(ctx, value, recurseTimes) {
600600
if (base.indexOf('\n at') === -1) {
601601
base = `[${base}]`;
602602
}
603+
// The message and the stack have to be indented as well!
604+
if (ctx.indentationLvl !== 0) {
605+
const indentation = ' '.repeat(ctx.indentationLvl);
606+
base = formatError(value).replace(/\n/g, `\n${indentation}`);
607+
}
603608
if (keyLength === 0)
604609
return base;
605610
} else if (isAnyArrayBuffer(value)) {

test/message/util_inspect_error.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
require('../common');
4+
const util = require('util');
5+
6+
const err = new Error('foo\nbar');
7+
8+
console.log(util.inspect({ err, nested: { err } }, { compact: true }));
9+
console.log(util.inspect({ err, nested: { err } }, { compact: false }));
10+
11+
err.foo = 'bar';
12+
console.log(util.inspect(err, { compact: true, breakLength: 5 }));

test/message/util_inspect_error.out

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{ err:
2+
Error: foo
3+
bar
4+
at *util_inspect_error*
5+
at *
6+
at *
7+
at *
8+
at *
9+
at *
10+
at *
11+
at *
12+
at *
13+
nested:
14+
{ err:
15+
Error: foo
16+
bar
17+
at *util_inspect_error*
18+
at *
19+
at *
20+
at *
21+
at *
22+
at *
23+
at *
24+
at *
25+
at * } }
26+
{
27+
err: Error: foo
28+
bar
29+
at *util_inspect_error*
30+
at *
31+
at *
32+
at *
33+
at *
34+
at *
35+
at *
36+
at *
37+
at *,
38+
nested: {
39+
err: Error: foo
40+
bar
41+
at *util_inspect_error*
42+
at *
43+
at *
44+
at *
45+
at *
46+
at *
47+
at *
48+
at *
49+
at *
50+
}
51+
}
52+
{ Error: foo
53+
bar
54+
at *util_inspect_error*
55+
at *
56+
at *
57+
at *
58+
at *
59+
at *
60+
at *
61+
at *
62+
at *
63+
foo: 'bar' }

0 commit comments

Comments
 (0)