Skip to content

Commit

Permalink
errors: make input mandatory
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
BridgeAR committed Mar 25, 2018
1 parent b38c81c commit 28e4e43
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 28e4e43

Please sign in to comment.