From f7c96856f90f4fcd53bc3c166736a53e9c25d729 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 29 Mar 2019 14:19:14 +0100 Subject: [PATCH] util: improve error property inspection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes sure that errors that contain extra properties show those properties on a separate line. PR-URL: https://github.com/nodejs/node/pull/26984 Reviewed-By: Michaƫl Zasso Reviewed-By: Matteo Collina --- lib/internal/util/inspect.js | 9 +++++---- test/parallel/test-repl-pretty-stack.js | 4 ++-- test/parallel/test-repl.js | 4 +++- test/parallel/test-util-inspect.js | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 9280e2ac5faab1..87e53d62f64f05 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1342,7 +1342,7 @@ function formatProperty(ctx, value, recurseTimes, key, type) { return `${name}:${extra}${str}`; } -function isBelowBreakLength(ctx, output, start) { +function isBelowBreakLength(ctx, output, start, base) { // Each entry is separated by at least a comma. Thus, we start with a total // length of at least `output.length`. In addition, some cases have a // whitespace in-between each other that is added to the total as well. @@ -1359,7 +1359,8 @@ function isBelowBreakLength(ctx, output, start) { return false; } } - return true; + // Do not line up properties on the same line if `base` contains line breaks. + return base === '' || !base.includes('\n'); } function reduceToSingleString(ctx, output, base, braces, combine = false) { @@ -1370,7 +1371,7 @@ function reduceToSingleString(ctx, output, base, braces, combine = false) { // that may reduce `breakLength`. const start = output.length + ctx.indentationLvl + braces[0].length + base.length + 10; - if (isBelowBreakLength(ctx, output, start)) { + if (isBelowBreakLength(ctx, output, start, base)) { return `${base ? `${base} ` : ''}${braces[0]} ${join(output, ', ')} ` + braces[1]; } @@ -1382,7 +1383,7 @@ function reduceToSingleString(ctx, output, base, braces, combine = false) { } // Line up all entries on a single line in case the entries do not exceed // `breakLength`. - if (isBelowBreakLength(ctx, output, 0)) { + if (isBelowBreakLength(ctx, output, 0, base)) { return `${braces[0]}${base ? ` ${base}` : ''} ${join(output, ', ')} ` + braces[1]; } diff --git a/test/parallel/test-repl-pretty-stack.js b/test/parallel/test-repl-pretty-stack.js index 4bf18fa1c2da92..454c9026481cb5 100644 --- a/test/parallel/test-repl-pretty-stack.js +++ b/test/parallel/test-repl-pretty-stack.js @@ -48,12 +48,12 @@ const tests = [ { command: '(() => { const err = Error(\'Whoops!\'); ' + 'err.foo = \'bar\'; throw err; })()', - expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: \'bar\' }\n', + expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22\n foo: \'bar\' }\n', }, { command: '(() => { const err = Error(\'Whoops!\'); ' + 'err.foo = \'bar\'; throw err; })()', - expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: ' + + expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22\n foo: ' + "\u001b[32m'bar'\u001b[39m }\n", useColors: true }, diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index ad674cb3ab00f4..3cfc105201a9e3 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -540,7 +540,9 @@ const errorTests = [ /^ at .*/, /^ at .*/, /^ at .*/, - /^ at .*/ + /^ at .*/, + " code: 'MODULE_NOT_FOUND',", + " requireStack: [ '' ] }" ] }, // REPL should handle quotes within regexp literal in multiline mode diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 300ab5524a5c3a..1de812286a2c28 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -578,7 +578,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324'); ); assert.strictEqual( util.inspect(err2, { compact: true }), - '{ [Error: foo\nbar] bar: true }' + '{ [Error: foo\nbar]\n bar: true }' ); assert.strictEqual( util.inspect(err, { compact: true, breakLength: 5 }),