Skip to content

Commit

Permalink
internal/errors: improve ERR_INVALID_ARG_TYPE
Browse files Browse the repository at this point in the history
The error message might be misleading if a 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.
  • Loading branch information
BridgeAR committed Jun 20, 2017
1 parent 1fcb76e commit adc7c88
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 adc7c88

Please sign in to comment.