Skip to content

Commit f7c9685

Browse files
committed
util: improve error property inspection
This makes sure that errors that contain extra properties show those properties on a separate line. PR-URL: #26984 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 14b2db0 commit f7c9685

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

lib/internal/util/inspect.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ function formatProperty(ctx, value, recurseTimes, key, type) {
13421342
return `${name}:${extra}${str}`;
13431343
}
13441344

1345-
function isBelowBreakLength(ctx, output, start) {
1345+
function isBelowBreakLength(ctx, output, start, base) {
13461346
// Each entry is separated by at least a comma. Thus, we start with a total
13471347
// length of at least `output.length`. In addition, some cases have a
13481348
// whitespace in-between each other that is added to the total as well.
@@ -1359,7 +1359,8 @@ function isBelowBreakLength(ctx, output, start) {
13591359
return false;
13601360
}
13611361
}
1362-
return true;
1362+
// Do not line up properties on the same line if `base` contains line breaks.
1363+
return base === '' || !base.includes('\n');
13631364
}
13641365

13651366
function reduceToSingleString(ctx, output, base, braces, combine = false) {
@@ -1370,7 +1371,7 @@ function reduceToSingleString(ctx, output, base, braces, combine = false) {
13701371
// that may reduce `breakLength`.
13711372
const start = output.length + ctx.indentationLvl +
13721373
braces[0].length + base.length + 10;
1373-
if (isBelowBreakLength(ctx, output, start)) {
1374+
if (isBelowBreakLength(ctx, output, start, base)) {
13741375
return `${base ? `${base} ` : ''}${braces[0]} ${join(output, ', ')} ` +
13751376
braces[1];
13761377
}
@@ -1382,7 +1383,7 @@ function reduceToSingleString(ctx, output, base, braces, combine = false) {
13821383
}
13831384
// Line up all entries on a single line in case the entries do not exceed
13841385
// `breakLength`.
1385-
if (isBelowBreakLength(ctx, output, 0)) {
1386+
if (isBelowBreakLength(ctx, output, 0, base)) {
13861387
return `${braces[0]}${base ? ` ${base}` : ''} ${join(output, ', ')} ` +
13871388
braces[1];
13881389
}

test/parallel/test-repl-pretty-stack.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ const tests = [
4848
{
4949
command: '(() => { const err = Error(\'Whoops!\'); ' +
5050
'err.foo = \'bar\'; throw err; })()',
51-
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: \'bar\' }\n',
51+
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22\n foo: \'bar\' }\n',
5252
},
5353
{
5454
command: '(() => { const err = Error(\'Whoops!\'); ' +
5555
'err.foo = \'bar\'; throw err; })()',
56-
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: ' +
56+
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22\n foo: ' +
5757
"\u001b[32m'bar'\u001b[39m }\n",
5858
useColors: true
5959
},

test/parallel/test-repl.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,9 @@ const errorTests = [
540540
/^ at .*/,
541541
/^ at .*/,
542542
/^ at .*/,
543-
/^ at .*/
543+
/^ at .*/,
544+
" code: 'MODULE_NOT_FOUND',",
545+
" requireStack: [ '<repl>' ] }"
544546
]
545547
},
546548
// REPL should handle quotes within regexp literal in multiline mode

test/parallel/test-util-inspect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
578578
);
579579
assert.strictEqual(
580580
util.inspect(err2, { compact: true }),
581-
'{ [Error: foo\nbar] bar: true }'
581+
'{ [Error: foo\nbar]\n bar: true }'
582582
);
583583
assert.strictEqual(
584584
util.inspect(err, { compact: true, breakLength: 5 }),

0 commit comments

Comments
 (0)