Permalink
Browse files

dns: return TypeError on invalid resolve() input

Synchronize the argument list for `dns.resolve()` with what's in the
documentation.

Improve the error for a bad `rrtype` to be a `TypeError` rather than an
`Error`.

PR-URL: #13090
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information...
Trott committed May 17, 2017
1 parent a94b98e commit 758a17f1d5f5985f6d5e3823e0c4759e78252c52
Showing with 10 additions and 13 deletions.
  1. +8 −9 lib/dns.js
  2. +2 −4 test/parallel/test-dns.js
View
@@ -291,22 +291,21 @@ resolveMap.NAPTR = resolver('queryNaptr');
resolveMap.SOA = resolver('querySoa');
function resolve(hostname, type_, callback_) {
var resolver, callback;
if (typeof type_ === 'string') {
resolver = resolveMap[type_];
callback = callback_;
} else if (typeof type_ === 'function') {
function resolve(hostname, rrtype, callback) {
var resolver;
if (typeof rrtype === 'string') {
resolver = resolveMap[rrtype];
} else if (typeof rrtype === 'function') {
resolver = resolveMap.A;
callback = type_;
callback = rrtype;
} else {
throw new Error('"type" argument must be a string');
throw new TypeError('"rrtype" argument must be a string');
}
if (typeof resolver === 'function') {
return resolver(hostname, callback);
} else {
throw new Error(`Unknown type "${type_}"`);
throw new Error(`Unknown type "${rrtype}"`);
}
}
@@ -91,10 +91,8 @@ assert.doesNotThrow(() => dns.setServers([]));
assert.deepStrictEqual(dns.getServers(), []);
assert.throws(() => {
dns.resolve('test.com', [], common.mustNotCall());
}, function(err) {
return !(err instanceof TypeError);
}, 'Unexpected error');
dns.resolve('example.com', [], common.mustNotCall());
}, /^TypeError: "rrtype" argument must be a string$/);
// dns.lookup should accept only falsey and string values
{

0 comments on commit 758a17f

Please sign in to comment.