Skip to content

Commit

Permalink
util: improve error property inspection
Browse files Browse the repository at this point in the history
This makes sure that errors that contain extra properties show those
properties on a separate line.

PR-URL: #26984
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
BridgeAR committed Apr 3, 2019
1 parent 14b2db0 commit f7c9685
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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];
}
Expand All @@ -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];
}
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-repl-pretty-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,9 @@ const errorTests = [
/^ at .*/,
/^ at .*/,
/^ at .*/,
/^ at .*/
/^ at .*/,
" code: 'MODULE_NOT_FOUND',",
" requireStack: [ '<repl>' ] }"
]
},
// REPL should handle quotes within regexp literal in multiline mode
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down

0 comments on commit f7c9685

Please sign in to comment.