Skip to content

Commit

Permalink
errors: improve ERR_INVALID_ARG_TYPE
Browse files Browse the repository at this point in the history
The error message might be misleading if an object property
was the issue and not the argument itself.

Fix this by checking if a argument or a property is passed
to the handler function.

PR-URL: #13730
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
BridgeAR authored and refack committed Jun 22, 2017
1 parent 2a46e57 commit 3e17884
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ E('ERR_V8BREAKITERATOR', 'full ICU data not installed. ' +
function invalidArgType(name, expected, actual) {
const assert = lazyAssert();
assert(name, 'name is required');
var msg = `The "${name}" argument must be ${oneOf(expected, 'type')}`;
const type = name.includes('.') ? 'property' : 'argument';
var msg = `The "${name}" ${type} must be ${oneOf(expected, 'type')}`;
if (arguments.length >= 3) {
msg += `. Received type ${actual !== null ? typeof actual : 'null'}`;
}
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-process-cpuUsage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ for (let i = 0; i < 10; i++) {
const invalidUserArgument = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "preValue.user" argument must be of type Number'
message: 'The "preValue.user" property must be of type Number'
});

const invalidSystemArgument = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "preValue.system" argument must be of type Number'
message: 'The "preValue.system" property must be of type Number'
});


Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-util-inherits.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ assert.throws(function() {
}, common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "superCtor.prototype" argument must be of type function'
message: 'The "superCtor.prototype" property must be of type function'
})
);
assert.throws(function() {
Expand Down

0 comments on commit 3e17884

Please sign in to comment.