errors: make input mandatory

Using ERR_INVALID_ARG_TYPE will now require the received value as
well. This makes sure the errors are always expressive. It also
drops support for using an array as name argument.

PR-URL: https://github.com/nodejs/node/pull/19445
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Ruben Bridgewater 2018-03-19 14:21:27 +01:00
parent b38c81cb44
commit 28e4e43e51
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
1 changed files with 5 additions and 9 deletions

View File

@ -936,7 +936,8 @@ function sysError(code, syscall, path, dest,
messages.set('ERR_SYSTEM_ERROR', sysError);
function invalidArgType(name, expected, actual) {
internalAssert(name, 'name is required');
internalAssert(typeof name === 'string');
internalAssert(arguments.length === 3);
// determiner: 'must be' or 'must not be'
let determiner;
@ -948,21 +949,16 @@ function invalidArgType(name, expected, actual) {
}
let msg;
if (Array.isArray(name)) {
var names = name.map((val) => `"${val}"`).join(', ');
msg = `The ${names} arguments ${determiner} ${oneOf(expected, 'type')}`;
} else if (name.endsWith(' argument')) {
// for the case like 'first argument'
if (name.endsWith(' argument')) {
// For cases like 'first argument'
msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
} else {
const type = name.includes('.') ? 'property' : 'argument';
msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
}
// If actual value received, output it
// TODO(BridgeAR): Improve the output by showing `null` and similar.
if (arguments.length === 3)
msg += `. Received type ${typeof actual}`;
msg += `. Received type ${typeof actual}`;
return msg;
}