Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 34 additions & 20 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Copy link
Copy Markdown
Contributor

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.

err = 'ENOTFOUND';
}
var ex = null;
Expand Down Expand Up @@ -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');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 &&
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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}]`);
}
}

Expand Down
59 changes: 59 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Contributor

@mscdex mscdex Mar 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 111294 here should be 11294 instead? Also it's a little bit vague about what this comment is referring to, perhaps it can just be removed?


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));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid these functional Array methods for performance.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline at end of file.