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 5ce85a7

Browse filesBrowse files
BridgeARMylesBorins
authored andcommitted
util: wrap error in brackets without stack
This aligns the visualization of an error with no stack traces set to zero just as it is done in case the error has no stack trace. 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 8a17a25 commit 5ce85a7
Copy full SHA for 5ce85a7

File tree

Expand file treeCollapse file tree

2 files changed

+25
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+25
-9
lines changed
Open diff view settings
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,13 @@ function formatValue(ctx, value, recurseTimes, ln) {
595595
base = `${dateToISOString.call(value)}`;
596596
} else if (isError(value)) {
597597
// Make error with message first say the error
598+
base = formatError(value);
599+
// Wrap the error in brackets in case it has no stack trace.
600+
if (base.indexOf('\n at') === -1) {
601+
base = `[${base}]`;
602+
}
598603
if (keyLength === 0)
599-
return formatError(value);
600-
base = `${formatError(value)}`;
604+
return base;
601605
} else if (isAnyArrayBuffer(value)) {
602606
// Fast path for ArrayBuffer and SharedArrayBuffer.
603607
// Can't do the same for DataView because it has a non-primitive
@@ -749,7 +753,7 @@ function formatPrimitive(fn, value, ctx) {
749753
}
750754

751755
function formatError(value) {
752-
return value.stack || `[${errorToString.call(value)}]`;
756+
return value.stack || errorToString.call(value);
753757
}
754758

755759
function formatObject(ctx, value, recurseTimes, keys) {
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+18-6Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,12 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
496496

497497
// Exceptions should print the error message, not '{}'.
498498
{
499-
const errors = [];
500-
errors.push(new Error());
501-
errors.push(new Error('FAIL'));
502-
errors.push(new TypeError('FAIL'));
503-
errors.push(new SyntaxError('FAIL'));
504-
errors.forEach((err) => {
499+
[
500+
new Error(),
501+
new Error('FAIL'),
502+
new TypeError('FAIL'),
503+
new SyntaxError('FAIL')
504+
].forEach((err) => {
505505
assert.strictEqual(util.inspect(err), err.stack);
506506
});
507507
try {
@@ -515,6 +515,18 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
515515
assert(ex.includes('[message]'));
516516
}
517517

518+
{
519+
const tmp = Error.stackTraceLimit;
520+
Error.stackTraceLimit = 0;
521+
const err = new Error('foo');
522+
assert.strictEqual(util.inspect(err), '[Error: foo]');
523+
assert(err.stack);
524+
delete err.stack;
525+
assert(!err.stack);
526+
assert.strictEqual(util.inspect(err), '[Error: foo]');
527+
Error.stackTraceLimit = tmp;
528+
}
529+
518530
// Doesn't capture stack trace.
519531
{
520532
function BadCustomError(msg) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.