Skip to content

Commit

Permalink
util: fix inspected stack indentation
Browse files Browse the repository at this point in the history
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 <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>
  • Loading branch information
BridgeAR committed May 21, 2018
1 parent 8de8372 commit e852289
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,11 @@ function formatValue(ctx, value, recurseTimes) {
if (base.indexOf('\n at') === -1) {
base = `[${base}]`;
}
// The message and the stack have to be indented as well!
if (ctx.indentationLvl !== 0) {
const indentation = ' '.repeat(ctx.indentationLvl);
base = formatError(value).replace(/\n/g, `\n${indentation}`);
}
if (keyLength === 0)
return base;
} else if (isAnyArrayBuffer(value)) {
Expand Down
12 changes: 12 additions & 0 deletions test/message/util_inspect_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

require('../common');
const util = require('util');

const err = new Error('foo\nbar');

console.log(util.inspect({ err, nested: { err } }, { compact: true }));
console.log(util.inspect({ err, nested: { err } }, { compact: false }));

err.foo = 'bar';
console.log(util.inspect(err, { compact: true, breakLength: 5 }));
63 changes: 63 additions & 0 deletions test/message/util_inspect_error.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{ err:
Error: foo
bar
at *util_inspect_error*
at *
at *
at *
at *
at *
at *
at *
at *
nested:
{ err:
Error: foo
bar
at *util_inspect_error*
at *
at *
at *
at *
at *
at *
at *
at * } }
{
err: Error: foo
bar
at *util_inspect_error*
at *
at *
at *
at *
at *
at *
at *
at *,
nested: {
err: Error: foo
bar
at *util_inspect_error*
at *
at *
at *
at *
at *
at *
at *
at *
}
}
{ Error: foo
bar
at *util_inspect_error*
at *
at *
at *
at *
at *
at *
at *
at *
foo: 'bar' }

0 comments on commit e852289

Please sign in to comment.