Skip to content

Commit e852289

Browse files
committed
util: fix inspected stack indentation
Error stacks and multiline error messages were not correct indented. This is fixed by this patch. PR-URL: nodejs#20802 Refs: nodejs#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 8de8372 commit e852289

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

lib/util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,11 @@ function formatValue(ctx, value, recurseTimes) {
590590
if (base.indexOf('\n at') === -1) {
591591
base = `[${base}]`;
592592
}
593+
// The message and the stack have to be indented as well!
594+
if (ctx.indentationLvl !== 0) {
595+
const indentation = ' '.repeat(ctx.indentationLvl);
596+
base = formatError(value).replace(/\n/g, `\n${indentation}`);
597+
}
593598
if (keyLength === 0)
594599
return base;
595600
} else if (isAnyArrayBuffer(value)) {

test/message/util_inspect_error.js

Lines changed: 12 additions & 0 deletions
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 }));
Lines changed: 63 additions & 0 deletions
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)