util: improve error property inspection

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 <targos@protonmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Ruben Bridgewater 2019-03-29 14:19:14 +01:00
parent 14b2db0145
commit f7c96856f9
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
4 changed files with 11 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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