Skip to content

Commit c041a2e

Browse files
committed
util: improve error inspection
When inspecting errors with extra properties while setting the compact option to false, it will now return: [Error: foo] { at repl:1:5 at Script.runInThisContext (vm.js:89:20) bla: true } Instead of: Error: foo at repl:1:5 at Script.runInThisContext (vm.js:91:20) { bla: true } PR-URL: nodejs#20802 Refs: nodejs#20253 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent e852289 commit c041a2e

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

lib/util.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,8 @@ function formatValue(ctx, value, recurseTimes) {
587587
// Make error with message first say the error
588588
base = formatError(value);
589589
// Wrap the error in brackets in case it has no stack trace.
590-
if (base.indexOf('\n at') === -1) {
590+
const stackStart = base.indexOf('\n at');
591+
if (stackStart === -1) {
591592
base = `[${base}]`;
592593
}
593594
// The message and the stack have to be indented as well!
@@ -597,6 +598,11 @@ function formatValue(ctx, value, recurseTimes) {
597598
}
598599
if (keyLength === 0)
599600
return base;
601+
602+
if (ctx.compact === false && stackStart !== -1) {
603+
braces[0] += `${base.slice(stackStart)}`;
604+
base = `[${base.slice(0, stackStart)}]`;
605+
}
600606
} else if (isAnyArrayBuffer(value)) {
601607
// Fast path for ArrayBuffer and SharedArrayBuffer.
602608
// Can't do the same for DataView because it has a non-primitive

test/parallel/test-repl-underscore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function testError() {
174174

175175
// The error, both from the original throw and the `_error` echo.
176176
'Error: foo',
177-
'Error: foo',
177+
'[Error: foo]',
178178

179179
// The sync error, with individual property echoes
180180
/Error: ENOENT: no such file or directory, scandir '.*nonexistent.*'/,

test/parallel/test-util-inspect.js

+40-2
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,49 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
519519
const tmp = Error.stackTraceLimit;
520520
Error.stackTraceLimit = 0;
521521
const err = new Error('foo');
522-
assert.strictEqual(util.inspect(err), '[Error: foo]');
522+
const err2 = new Error('foo\nbar');
523+
assert.strictEqual(util.inspect(err, { compact: true }), '[Error: foo]');
523524
assert(err.stack);
524525
delete err.stack;
525526
assert(!err.stack);
526-
assert.strictEqual(util.inspect(err), '[Error: foo]');
527+
assert.strictEqual(util.inspect(err, { compact: true }), '[Error: foo]');
528+
assert.strictEqual(
529+
util.inspect(err2, { compact: true }),
530+
'[Error: foo\nbar]'
531+
);
532+
533+
err.bar = true;
534+
err2.bar = true;
535+
536+
assert.strictEqual(
537+
util.inspect(err, { compact: true }),
538+
'{ [Error: foo] bar: true }'
539+
);
540+
assert.strictEqual(
541+
util.inspect(err2, { compact: true }),
542+
'{ [Error: foo\nbar] bar: true }'
543+
);
544+
assert.strictEqual(
545+
util.inspect(err, { compact: true, breakLength: 5 }),
546+
'{ [Error: foo]\n bar: true }'
547+
);
548+
assert.strictEqual(
549+
util.inspect(err, { compact: true, breakLength: 1 }),
550+
'{ [Error: foo]\n bar:\n true }'
551+
);
552+
assert.strictEqual(
553+
util.inspect(err2, { compact: true, breakLength: 5 }),
554+
'{ [Error: foo\nbar]\n bar: true }'
555+
);
556+
assert.strictEqual(
557+
util.inspect(err, { compact: false }),
558+
'[Error: foo] {\n bar: true\n}'
559+
);
560+
assert.strictEqual(
561+
util.inspect(err2, { compact: false }),
562+
'[Error: foo\nbar] {\n bar: true\n}'
563+
);
564+
527565
Error.stackTraceLimit = tmp;
528566
}
529567

0 commit comments

Comments
 (0)