From abe4c34c86ccbe06ff4ecd50774c0205009d4620 Mon Sep 17 00:00:00 2001 From: Kenan Sulayman Date: Fri, 7 Feb 2014 18:18:27 +0100 Subject: [PATCH 1/1] dns: verify argument is valid function in resolve MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Don't use argument as callback if it's not a valid callback function. Throw a valid exception instead explaining the issue. Adds to #7070 ("DNS — Throw meaningful error(s)"). --- lib/dns.js | 4 +++- test/simple/test-dns.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/dns.js b/lib/dns.js index cbba372..d77159d 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -185,9 +185,11 @@ exports.resolve = function(hostname, type_, callback_) { if (util.isString(type_)) { resolver = resolveMap[type_]; callback = callback_; - } else { + } else if (util.isFunction(type_)) { resolver = exports.resolve4; callback = type_; + } else { + throw new Error('Type must be a string'); } if (util.isFunction(resolver)) { diff --git a/test/simple/test-dns.js b/test/simple/test-dns.js index 9283bab..e7dce4b 100644 --- a/test/simple/test-dns.js +++ b/test/simple/test-dns.js @@ -60,3 +60,13 @@ assert.deepEqual(dns.getServers(), portsExpected); assert.doesNotThrow(function () { dns.setServers([]); }); assert.deepEqual(dns.getServers(), []); + +assert.throws( + function() { + dns.resolve('test.com', [], new Function); + }, + function(err) { + return !(err instanceof TypeError); + }, + "Unexpected error" +); -- 2.7.4