From 8fcdc9c1d5d112b0b1feee81a2bcf076f82bccfe Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 17 May 2017 13:37:30 -0700 Subject: [PATCH] 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`. --- lib/dns.js | 17 ++++++++--------- test/parallel/test-dns.js | 6 ++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/dns.js b/lib/dns.js index 49cd9c929415b8..5b3ac41ba95da6 100644 --- a/lib/dns.js +++ b/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}"`); } } diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index b409bb5e660d83..a605e76c1d4f16 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -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 {