Skip to content

Commit c8c9211

Browse files
BridgeARMylesBorins
authored andcommitted
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: #20802 Refs: #20253 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent f0d6a37 commit c8c9211

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎lib/util.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ function formatValue(ctx, value, recurseTimes) {
597597
// Make error with message first say the error
598598
base=formatError(value);
599599
// Wrap the error in brackets in case it has no stack trace.
600-
if(base.indexOf('\n at')===-1){
600+
conststackStart=base.indexOf('\n at');
601+
if(stackStart===-1){
601602
base=`[${base}]`;
602603
}
603604
// The message and the stack have to be indented as well!
@@ -607,6 +608,11 @@ function formatValue(ctx, value, recurseTimes) {
607608
}
608609
if(keyLength===0)
609610
returnbase;
611+
612+
if(ctx.compact===false&&stackStart!==-1){
613+
braces[0]+=`${base.slice(stackStart)}`;
614+
base=`[${base.slice(0,stackStart)}]`;
615+
}
610616
}elseif(isAnyArrayBuffer(value)){
611617
// Fast path for ArrayBuffer and SharedArrayBuffer.
612618
// Can't do the same for DataView because it has a non-primitive

‎test/parallel/test-repl-underscore.js‎

Lines changed: 1 addition & 1 deletion
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:nosuchfileordirectory,scandir'.*nonexistent.*'/,

‎test/parallel/test-util-inspect.js‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,49 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
519519
consttmp=Error.stackTraceLimit;
520520
Error.stackTraceLimit=0;
521521
consterr=newError('foo');
522-
assert.strictEqual(util.inspect(err),'[Error: foo]');
522+
consterr2=newError('foo\nbar');
523+
assert.strictEqual(util.inspect(err,{compact: true}),'[Error: foo]');
523524
assert(err.stack);
524525
deleteerr.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)