diff --git a/lib/util.js b/lib/util.js index 676f43499e830c..36a74b2e3041ab 100644 --- a/lib/util.js +++ b/lib/util.js @@ -600,6 +600,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)) { diff --git a/test/message/util_inspect_error.js b/test/message/util_inspect_error.js new file mode 100644 index 00000000000000..20affd6c711fd8 --- /dev/null +++ b/test/message/util_inspect_error.js @@ -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 })); diff --git a/test/message/util_inspect_error.out b/test/message/util_inspect_error.out new file mode 100644 index 00000000000000..406d8112ce2599 --- /dev/null +++ b/test/message/util_inspect_error.out @@ -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' }