util: fix inspected stack indentation

Error stacks and multiline error messages were not correct indented.
This is fixed by this patch.

PR-URL: https://github.com/nodejs/node/pull/20802
Refs: https://github.com/nodejs/node/issues/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>
This commit is contained in:
Ruben Bridgewater 2018-05-17 03:26:21 +02:00
parent 8de83725ac
commit e852289802
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
3 changed files with 80 additions and 0 deletions

View File

@ -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)) {

View File

@ -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 }));

View File

@ -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' }