Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors, url: migrate to use internal/errors.js #11360

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,15 @@ found [here][online].
encountered by [`http`][] or [`net`][] -- often a sign that a `socket.end()`
was not properly called.

<a id="nodejs-error-codes"></a>
## Node.js Error Codes

<a id="ERR_INVALID_ARG_TYPE"></a>
### 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
Expand Down
25 changes: 25 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ${actual === null ? 'null' : typeof actual}`;
}
return msg;
}
6 changes: 3 additions & 3 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ?
Expand Down