Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c8c9211

Browse filesBrowse 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
Copy full SHA for c8c9211

File tree

Expand file treeCollapse file tree

3 files changed

+48
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+48
-4
lines changed
Open diff view settings
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+7-1Lines 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+
const stackStart = 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
return base;
611+
612+
if (ctx.compact === false && stackStart !== -1) {
613+
braces[0] += `${base.slice(stackStart)}`;
614+
base = `[${base.slice(0, stackStart)}]`;
615+
}
610616
} else if (isAnyArrayBuffer(value)) {
611617
// Fast path for ArrayBuffer and SharedArrayBuffer.
612618
// Can't do the same for DataView because it has a non-primitive
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-repl-underscore.js
+1-1Lines 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: no such file or directory, scandir '.*nonexistent.*'/,
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+40-2Lines 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
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.