-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
dns, net, internal/net: migrate to internals/error #11738
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ const util = require('util'); | |
| const cares = process.binding('cares_wrap'); | ||
| const uv = process.binding('uv'); | ||
| const internalNet = require('internal/net'); | ||
| const errors = require('internal/errors'); | ||
|
|
||
| const GetAddrInfoReqWrap = cares.GetAddrInfoReqWrap; | ||
| const GetNameInfoReqWrap = cares.GetNameInfoReqWrap; | ||
|
|
@@ -18,8 +19,8 @@ function errnoException(err, syscall, hostname) { | |
| // FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass | ||
| // the true error to the user. ENOTFOUND is not even a proper POSIX error! | ||
| if (err === uv.UV_EAI_MEMORY || | ||
| err === uv.UV_EAI_NODATA || | ||
| err === uv.UV_EAI_NONAME) { | ||
| err === uv.UV_EAI_NODATA || | ||
| err === uv.UV_EAI_NONAME) { | ||
| err = 'ENOTFOUND'; | ||
| } | ||
| var ex = null; | ||
|
|
@@ -106,30 +107,33 @@ function lookup(hostname, options, callback) { | |
|
|
||
| // Parse arguments | ||
| if (hostname && typeof hostname !== 'string') { | ||
| throw new TypeError('Invalid arguments: ' + | ||
| 'hostname must be a string or falsey'); | ||
| // throw new TypeError('Invalid arguments: ' + 'hostname must be a string or falsey'); | ||
| throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'hostname', ['string', 'buffer']); | ||
| } else if (typeof options === 'function') { | ||
| callback = options; | ||
| family = 0; | ||
| } else if (typeof callback !== 'function') { | ||
| throw new TypeError('Invalid arguments: callback must be passed'); | ||
| // throw new TypeError('Invalid arguments: callback must be passed'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be removed. |
||
| throw new errors.TypeError('ERR_INVALID_CALLBACK'); | ||
| } else if (options !== null && typeof options === 'object') { | ||
| hints = options.hints >>> 0; | ||
| family = options.family >>> 0; | ||
| all = options.all === true; | ||
|
|
||
| if (hints !== 0 && | ||
| hints !== cares.AI_ADDRCONFIG && | ||
| hints !== cares.AI_V4MAPPED && | ||
| hints !== (cares.AI_ADDRCONFIG | cares.AI_V4MAPPED)) { | ||
| throw new TypeError('Invalid argument: hints must use valid flags'); | ||
| hints !== cares.AI_ADDRCONFIG && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto about reverting the indentation changes. |
||
| hints !== cares.AI_V4MAPPED && | ||
| hints !== (cares.AI_ADDRCONFIG | cares.AI_V4MAPPED)) { | ||
| // throw new TypeError('Invalid argument: hints must use valid flags'); | ||
| throw new errors.Error('ERR_INVALID_FLAGS', 'options.hints'); | ||
| } | ||
| } else { | ||
| family = options >>> 0; | ||
| } | ||
|
|
||
| if (family !== 0 && family !== 4 && family !== 6) | ||
| throw new TypeError('Invalid argument: family must be 4 or 6'); | ||
| // throw new TypeError('Invalid argument: family must be 4 or 6'); | ||
| throw new errors.Error('ERR_INVALID_ARG_VALUE', 'options.family', ['4', '6']); | ||
|
|
||
| callback = makeAsync(callback); | ||
|
|
||
|
|
@@ -180,16 +184,20 @@ function onlookupservice(err, host, service) { | |
| // lookupService(address, port, callback) | ||
| function lookupService(host, port, callback) { | ||
| if (arguments.length !== 3) | ||
| throw new Error('Invalid arguments'); | ||
| // throw new Error('Invalid arguments'); | ||
| throw new errors.Error('ERR_INSUFFICIENT_ARGS'); | ||
|
|
||
| if (isIP(host) === 0) | ||
| throw new TypeError('"host" argument needs to be a valid IP address'); | ||
| // throw new TypeError('"host" argument needs to be a valid IP address'); | ||
| throw new errors.Error('ERR_INVALID_IP', 'host', host) | ||
|
|
||
| if (!isLegalPort(port)) | ||
| throw new TypeError(`"port" should be >= 0 and < 65536, got "${port}"`); | ||
| // throw new TypeError(`"port" should be >= 0 and < 65536, got "${port}"`); | ||
| throw new errors.Error('ERR_INVALID_PORT', 'port', port); | ||
|
|
||
| if (typeof callback !== 'function') | ||
| throw new TypeError('"callback" argument must be a function'); | ||
| // throw new TypeError('"callback" argument must be a function'); | ||
| throw new errors.TypeError('ERR_INVALID_CALLBACK'); | ||
|
|
||
| port = +port; | ||
| callback = makeAsync(callback); | ||
|
|
@@ -230,9 +238,11 @@ function resolver(bindingName) { | |
| } | ||
|
|
||
| if (typeof name !== 'string') { | ||
| throw new Error('"name" argument must be a string'); | ||
| // throw new Error('"name" argument must be a string'); | ||
| throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string'); | ||
| } else if (typeof callback !== 'function') { | ||
| throw new Error('"callback" argument must be a function'); | ||
| // throw new Error('"callback" argument must be a function'); | ||
| throw new errors.Error('ERR_INVALID_CALLBACK'); | ||
| } | ||
|
|
||
| callback = makeAsync(callback); | ||
|
|
@@ -272,13 +282,15 @@ function resolve(hostname, type_, callback_) { | |
| resolver = resolveMap.A; | ||
| callback = type_; | ||
| } else { | ||
| throw new Error('"type" argument must be a string'); | ||
| // throw new Error('"type" argument must be a string'); | ||
| throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'type', 'string'); | ||
| } | ||
|
|
||
| if (typeof resolver === 'function') { | ||
| return resolver(hostname, callback); | ||
| } else { | ||
| throw new Error(`Unknown type "${type_}"`); | ||
| // throw new Error(`Unknown type "${type_}"`); | ||
| throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'resolver', 'function'); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -313,7 +325,8 @@ function setServers(servers) { | |
| if (ipVersion !== 0) | ||
| return newSet.push([ipVersion, s]); | ||
|
|
||
| throw new Error(`IP address is not properly formatted: ${serv}`); | ||
| // throw new Error(`IP address is not properly formatted: ${serv}`); | ||
| throw new errors.Error('ERR_INVALID_IP', 'serv', serv) | ||
| }); | ||
|
|
||
| const errorNumber = cares.setServers(newSet); | ||
|
|
@@ -323,7 +336,8 @@ function setServers(servers) { | |
| cares.setServers(orig.join(',')); | ||
|
|
||
| var err = cares.strerror(errorNumber); | ||
| throw new Error(`c-ares failed to set servers: "${err}" [${servers}]`); | ||
| // throw new Error(`c-ares failed to set servers: "${err}" [${servers}]`); | ||
| throw new errors.Error('ERR_ASSERTION', `c-ares failed to set servers: "${err}" [${servers}]`); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,4 +85,63 @@ module.exports = exports = { | |
| // | ||
| // Note: Please try to keep these in alphabetical order | ||
| E('ERR_ASSERTION', (msg) => msg); | ||
| E('ERR_INVALID_ARG_TYPE', invalidArgType); | ||
| E('ERR_INVALID_CALLBACK', 'callback must be a function'); | ||
| E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed'); | ||
| E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed'); | ||
| E('ERR_UNKNOWN_STDIN_TYPE', 'Unknown stdin file type'); | ||
| E('ERR_UNKNOWN_STREAM_TYPE', 'Unknown stream file type'); | ||
| // Add new errors from here... | ||
| E('ERR_INVALID_ARG_VALUE', invalidArgValue); | ||
| E('ERR_INVALID_FLAGS', arg => `${arg} must use valid flags`); | ||
| E('ERR_INSUFFICIENT_ARGS', 'insufficient arguments were passed to function'); | ||
| E('ERR_INVALID_PORT', (arg, port) => `"${arg}" argument must be >= 0 and < 65536, got ${port}`); | ||
| E('ERR_INVALID_IP', (arg, ip) => `"${arg}" argument must be a valid IP address, got ${ip}`); | ||
|
|
||
| // Errors from 111294, port error from 11302 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
|
||
| 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 ${expected[0]}`; | ||
| } | ||
| } else { | ||
| msg += `type ${String(expected)}`; | ||
| } | ||
| if (arguments.length >= 3) { | ||
| msg += `. Received type ${actual !== null ? typeof actual : 'null'}`; | ||
| } | ||
| return msg; | ||
| } | ||
|
|
||
| function invalidArgValue(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)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid these functional Array methods for performance.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto about Array methods. |
||
| if (len > 1) { | ||
| msg += `${expected.slice(0, len - 1).join(', ')}, or ` + | ||
| expected[len - 1]; | ||
| } else { | ||
| msg += `${expected[0]}`; | ||
| } | ||
| } else { | ||
| msg += `${String(expected)}`; | ||
| } | ||
| if (arguments.length >= 3) { | ||
| msg += `. Received ${actual !== null ? typeof actual : 'null'}`; | ||
| } | ||
| return msg; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline at end of file. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be reverted, the conditionals should be lined up.