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 <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 38bc5fb commit f0d6a37

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎lib/util.js‎

Lines changed: 5 additions & 0 deletions
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+
constindentation=' '.repeat(ctx.indentationLvl);
606+
base=formatError(value).replace(/\n/g,`\n${indentation}`);
607+
}
603608
if(keyLength===0)
604609
returnbase;
605610
}elseif(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+
constutil=require('util');
5+
6+
consterr=newError('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)