Skip to content

Commit b564255

Browse files
joyeecheungMayaLekova
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. PR-URL: nodejs#18358 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jon Moss <[email protected]>
1 parent 01fcb3e commit b564255

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

lib/internal/errors.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ const { kMaxLength } = process.binding('buffer');
2323
const { defineProperty } = Object;
2424

2525
// Lazily loaded
26-
var util = null;
26+
var util_ = null;
2727
var buffer;
2828

29+
function lazyUtil() {
30+
if (!util_) {
31+
util_ = require('util');
32+
}
33+
return util_;
34+
}
35+
2936
function makeNodeError(Base) {
3037
return class NodeError extends Base {
3138
constructor(key, ...args) {
@@ -142,6 +149,7 @@ function createErrDiff(actual, expected, operator) {
142149
var lastPos = 0;
143150
var end = '';
144151
var skipped = false;
152+
const util = lazyUtil();
145153
const actualLines = util
146154
.inspect(actual, { compact: false }).split('\n');
147155
const expectedLines = util
@@ -262,13 +270,11 @@ class AssertionError extends Error {
262270
if (message != null) {
263271
super(message);
264272
} else {
265-
if (util === null) {
266-
util = require('util');
267-
if (process.stdout.isTTY && process.stdout.getColorDepth() !== 1) {
268-
green = '\u001b[32m';
269-
white = '\u001b[39m';
270-
red = '\u001b[31m';
271-
}
273+
const util = lazyUtil();
274+
if (process.stdout.isTTY && process.stdout.getColorDepth() !== 1) {
275+
green = '\u001b[32m';
276+
white = '\u001b[39m';
277+
red = '\u001b[31m';
272278
}
273279

274280
if (actual && actual.stack && actual instanceof Error)
@@ -333,7 +339,7 @@ function message(key, args) {
333339
if (typeof msg === 'function') {
334340
fmt = msg;
335341
} else {
336-
if (util === null) util = require('util');
342+
const util = lazyUtil();
337343
fmt = util.format;
338344
if (args === undefined || args.length === 0)
339345
return msg;
@@ -537,8 +543,14 @@ E('ERR_INSPECTOR_CLOSED', 'Session was closed');
537543
E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available');
538544
E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected');
539545
E('ERR_INVALID_ARG_TYPE', invalidArgType);
540-
E('ERR_INVALID_ARG_VALUE', (name, value) =>
541-
`The value "${String(value)}" is invalid for argument "${name}"`);
546+
E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
547+
const util = lazyUtil();
548+
let inspected = util.inspect(value);
549+
if (inspected.length > 128) {
550+
inspected = inspected.slice(0, 128) + '...';
551+
}
552+
return `The argument '${name}' ${reason}. Received ${inspected}`;
553+
}),
542554
E('ERR_INVALID_ARRAY_LENGTH',
543555
(name, len, actual) => {
544556
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
@@ -351,10 +351,20 @@ assert.strictEqual(
351351
}
352352

353353
{
354-
const error = new errors.Error('ERR_INVALID_ARG_VALUE', 'foo', 'bar');
354+
const error = new errors.Error('ERR_INVALID_ARG_VALUE', 'foo', '\u0000bar');
355355
assert.strictEqual(
356356
error.message,
357-
'The value "bar" is invalid for argument "foo"'
357+
'The argument \'foo\' is invalid. Received \'\\u0000bar\''
358+
);
359+
}
360+
361+
{
362+
const error = new errors.Error(
363+
'ERR_INVALID_ARG_VALUE',
364+
'foo', { a: 1 }, 'must have property \'b\'');
365+
assert.strictEqual(
366+
error.message,
367+
'The argument \'foo\' must have property \'b\'. Received { a: 1 }'
358368
);
359369
}
360370

0 commit comments

Comments
 (0)