From f0d6a37c5c9c750a9dd32899097ee468d0592bb0 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 17 May 2018 03:26:21 +0200 Subject: [PATCH] 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 Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Trivikram Kamat --- lib/util.js | 5 +++ test/message/util_inspect_error.js | 12 ++++++ test/message/util_inspect_error.out | 63 +++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 test/message/util_inspect_error.js create mode 100644 test/message/util_inspect_error.out 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' }