Skip to content

Commit

Permalink
dns: return TypeError on invalid resolve() input
Browse files Browse the repository at this point in the history
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 29, 2017
1 parent a94b98e commit 758a17f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
17 changes: 8 additions & 9 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`);
}
}

Expand Down
6 changes: 2 additions & 4 deletions test/parallel/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 758a17f

Please sign in to comment.