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

View File

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