Skip to content

Commit b3fe55a

Browse files
joyeecheungMylesBorins
authored andcommitted
errors: improve the description of ERR_INVALID_ARG_VALUE
- Allow user to customize why the argument is invalid - Display the argument with util.inspect so null bytes can be displayed properly. Backport-PR-URL: #18916 PR-URL: #18358 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me>
1 parent 28fa906 commit b3fe55a

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

lib/internal/errors.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ const { kMaxLength } = process.binding('buffer');
1717
const { defineProperty } = Object;
1818

1919
// Lazily loaded
20-
var util = null;
20+
var util_ = null;
21+
function lazyUtil() {
22+
if (!util_) {
23+
util_ = require('util');
24+
}
25+
return util_;
26+
}
2127

2228
function makeNodeError(Base) {
2329
return class NodeError extends Base {
@@ -68,11 +74,11 @@ class AssertionError extends Error {
6874
if (message) {
6975
super(message);
7076
} else {
77+
const util = lazyUtil();
7178
if (actual && actual.stack && actual instanceof Error)
7279
actual = `${actual.name}: ${actual.message}`;
7380
if (expected && expected.stack && expected instanceof Error)
7481
expected = `${expected.name}: ${expected.message}`;
75-
if (util === null) util = require('util');
7682
super(`${util.inspect(actual).slice(0, 128)} ` +
7783
`${operator} ${util.inspect(expected).slice(0, 128)}`);
7884
}
@@ -107,7 +113,7 @@ function message(key, args) {
107113
if (typeof msg === 'function') {
108114
fmt = msg;
109115
} else {
110-
if (util === null) util = require('util');
116+
const util = lazyUtil();
111117
fmt = util.format;
112118
if (args === undefined || args.length === 0)
113119
return msg;
@@ -263,8 +269,14 @@ E('ERR_INSPECTOR_CLOSED', 'Session was closed');
263269
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available');
264270
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected');
265271
E('ERR_INVALID_ARG_TYPE', invalidArgType);
266-
E('ERR_INVALID_ARG_VALUE', (name, value) =>
267-
`The value "${String(value)}" is invalid for argument "${name}"`);
272+
E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
273+
const util = lazyUtil();
274+
let inspected = util.inspect(value);
275+
if (inspected.length > 128) {
276+
inspected = inspected.slice(0, 128) + '...';
277+
}
278+
return `The argument '${name}' ${reason}. Received ${inspected}`;
279+
}),
268280
E('ERR_INVALID_ARRAY_LENGTH',
269281
(name, len, actual) => {
270282
internalAssert(typeof actual === 'number', 'actual must be a number');

test/parallel/test-internal-errors.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,20 @@ assert.strictEqual(
334334
}
335335

336336
{
337-
const error = new errors.Error('ERR_INVALID_ARG_VALUE', 'foo', 'bar');
337+
const error = new errors.Error('ERR_INVALID_ARG_VALUE', 'foo', '\u0000bar');
338338
assert.strictEqual(
339339
error.message,
340-
'The value "bar" is invalid for argument "foo"'
340+
'The argument \'foo\' is invalid. Received \'\\u0000bar\''
341+
);
342+
}
343+
344+
{
345+
const error = new errors.Error(
346+
'ERR_INVALID_ARG_VALUE',
347+
'foo', { a: 1 }, 'must have property \'b\'');
348+
assert.strictEqual(
349+
error.message,
350+
'The argument \'foo\' must have property \'b\'. Received { a: 1 }'
341351
);
342352
}
343353

0 commit comments

Comments
 (0)