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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
a94b98e7e1
commit
758a17f1d5
17
lib/dns.js
17
lib/dns.js
@ -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
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user