From 60e69baf9cf5c272fff9690f776e4e590172b60b Mon Sep 17 00:00:00 2001 From: Bougarfaoui El houcine Date: Mon, 13 Feb 2017 21:51:17 +0000 Subject: [PATCH 1/5] add ERR_INVALID_ARG_TYPE error --- lib/internal/errors.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index f2376f70371c60..c35a937d272579 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -86,3 +86,28 @@ module.exports = exports = { // Note: Please try to keep these in alphabetical order E('ERR_ASSERTION', (msg) => msg); // Add new errors from here... +E('ERR_INVALID_ARG_TYPE', invalidArgType); +// Add new errors from here... + +function invalidArgType(name, expected, actual) { + const assert = lazyAssert(); + assert(name, 'name is required'); + assert(expected, 'expected is required'); + var msg = `The "${name}" argument must be `; + if (Array.isArray(expected)) { + var len = expected.length; + expected = expected.map((i) => String(i)); + if (len > 1) { + msg += `one of type ${expected.slice(0, len - 1).join(', ')}/ +, or ${expected[len - 1]}`; + } else { + msg += `type ${String(expected[0])}`; + } + } else { + msg += `type ${String(expected)}`; + } + if (arguments.length >= 3) { + msg += `. Received type ${typeof actual}`; + } + return msg; +} From bd778bfe292b06998cc8be6a0ffe83dcd9d763ef Mon Sep 17 00:00:00 2001 From: Bougarfaoui El houcine Date: Mon, 13 Feb 2017 22:00:30 +0000 Subject: [PATCH 2/5] add ERR_INVALID_ARG_TYPE to doc --- doc/api/errors.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/doc/api/errors.md b/doc/api/errors.md index 640935da35e460..38ce52dea0e240 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -563,6 +563,15 @@ found [here][online]. encountered by [`http`][] or [`net`][] -- often a sign that a `socket.end()` was not properly called. + +## Node.js Error Codes + + +### ERR_INVALID_ARG_TYPE + +The `'ERR_INVALID_ARG_TYPE'` error code is used generically to identify that +an argument of the wrong type has been passed to a Node.js API. + [`fs.readdir`]: fs.html#fs_fs_readdir_path_options_callback [`fs.readFileSync`]: fs.html#fs_fs_readfilesync_file_options [`fs.unlink`]: fs.html#fs_fs_unlink_path_callback From 152c5fa5a5c73c3d73152f433850c0573028e595 Mon Sep 17 00:00:00 2001 From: Bougarfaoui El houcine Date: Mon, 13 Feb 2017 22:25:25 +0000 Subject: [PATCH 3/5] invalidArgType : fix if the actual arg is null --- lib/internal/errors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index c35a937d272579..03c7cc902e32bc 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -107,7 +107,7 @@ function invalidArgType(name, expected, actual) { msg += `type ${String(expected)}`; } if (arguments.length >= 3) { - msg += `. Received type ${typeof actual}`; + msg += `. Received type ${actual === null ? 'null' : typeof actual}`; } return msg; } From 1384821277bfb4165f6109f163886bb88ca547e2 Mon Sep 17 00:00:00 2001 From: Bougarfaoui El houcine Date: Mon, 13 Feb 2017 22:27:46 +0000 Subject: [PATCH 4/5] url : migrate to use intrenal/errors.js --- lib/url.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/url.js b/lib/url.js index 57f04d5f3fccac..3d64cb5451ab03 100644 --- a/lib/url.js +++ b/lib/url.js @@ -10,6 +10,7 @@ function importPunycode() { const { toASCII } = importPunycode(); +const errors = require('internal/errors'); const internalUrl = require('internal/url'); const encodeAuth = internalUrl.encodeAuth; exports.parse = urlParse; @@ -92,7 +93,7 @@ function urlParse(url, parseQueryString, slashesDenoteHost) { Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { if (typeof url !== 'string') { - throw new TypeError('Parameter "url" must be a string, not ' + typeof url); + throw new errors.TypeError('ERR_INVALID_ARG_TYPE','url','String',url); } // Copy chrome, IE, opera backslash-handling behavior. @@ -546,8 +547,7 @@ function urlFormat(obj, options) { if (typeof obj === 'string') { obj = urlParse(obj); } else if (typeof obj !== 'object' || obj === null) { - throw new TypeError('Parameter "urlObj" must be an object, not ' + - (obj === null ? 'null' : typeof obj)); + throw new errors.TypeError('ERR_INVALID_ARG_TYPE','obj','Object',obj); } else if (!(obj instanceof Url)) { var format = obj[internalUrl.formatSymbol]; return format ? From 748c4583b0bcd9f283254d581fdb74b1ff74d883 Mon Sep 17 00:00:00 2001 From: Bougarfaoui El houcine Date: Tue, 14 Feb 2017 21:06:15 +0000 Subject: [PATCH 5/5] fixing linter errors --- lib/url.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/url.js b/lib/url.js index 3d64cb5451ab03..bca937df460eb3 100644 --- a/lib/url.js +++ b/lib/url.js @@ -93,7 +93,7 @@ function urlParse(url, parseQueryString, slashesDenoteHost) { Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { if (typeof url !== 'string') { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE','url','String',url); + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'url', 'String', url); } // Copy chrome, IE, opera backslash-handling behavior. @@ -547,7 +547,7 @@ function urlFormat(obj, options) { if (typeof obj === 'string') { obj = urlParse(obj); } else if (typeof obj !== 'object' || obj === null) { - throw new errors.TypeError('ERR_INVALID_ARG_TYPE','obj','Object',obj); + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'obj', 'Object', obj); } else if (!(obj instanceof Url)) { var format = obj[internalUrl.formatSymbol]; return format ?