diff --git a/lib/dns.js b/lib/dns.js index 9ced633c423..04cf013f004 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -28,7 +28,7 @@ var uv = process.binding('uv'); var isIp = net.isIP; -function errnoException(err, syscall) { +function errnoException(err, syscall, hostname) { // FIXME(bnoordhuis) Remove this backwards compatibility shite and pass // the true error to the user. ENOTFOUND is not even a proper POSIX error! if (err === uv.UV_EAI_MEMORY || @@ -36,14 +36,19 @@ function errnoException(err, syscall) { err === uv.UV_EAI_NONAME) { err = 'ENOTFOUND'; } + var ex = null; if (typeof err === 'string') { // c-ares error code. - var ex = new Error(syscall + ' ' + err); + ex = new Error(syscall + ' ' + err); ex.code = err; ex.errno = err; ex.syscall = syscall; - return ex; + } else { + ex = util._errnoException(err, syscall); } - return util._errnoException(err, syscall); + if (hostname) { + ex.hostname = hostname; + } + return ex; } @@ -83,7 +88,7 @@ function makeAsync(callback) { function onlookup(err, addresses) { if (err) { - return this.callback(errnoException(err, 'getaddrinfo')); + return this.callback(errnoException(err, 'getaddrinfo', this.hostname)); } if (this.family) { this.callback(null, addresses[0], this.family); @@ -133,10 +138,11 @@ exports.lookup = function(hostname, family, callback) { var req = { callback: callback, family: family, + hostname: hostname, oncomplete: onlookup }; var err = cares.getaddrinfo(req, hostname, family); - if (err) throw errnoException(err, 'getaddrinfo'); + if (err) throw errnoException(err, 'getaddrinfo', hostname); callback.immediately = true; return req; @@ -145,7 +151,7 @@ exports.lookup = function(hostname, family, callback) { function onresolve(err, result) { if (err) - this.callback(errnoException(err, this.bindingName)); + this.callback(errnoException(err, this.bindingName, this.hostname)); else this.callback(null, result); } @@ -159,6 +165,7 @@ function resolver(bindingName) { var req = { bindingName: bindingName, callback: callback, + hostname: name, oncomplete: onresolve }; var err = binding(req, name); diff --git a/test/internet/test-dns.js b/test/internet/test-dns.js index 5f360b4aeec..cc69106c524 100644 --- a/test/internet/test-dns.js +++ b/test/internet/test-dns.js @@ -393,6 +393,45 @@ TEST(function test_lookup_localhost_ipv4(done) { }); +TEST(function test_reverse_failure(done) { + var req = dns.reverse('0.0.0.0', function(err) { + assert(err instanceof Error); + assert.strictEqual(err.code, 'ENOTFOUND'); // Silly error code... + assert.strictEqual(err.hostname, '0.0.0.0'); + + done(); + }); + + checkWrap(req); +}); + + +TEST(function test_lookup_failure(done) { + var req = dns.lookup('nosuchhostimsure', function(err) { + assert(err instanceof Error); + assert.strictEqual(err.code, 'ENOTFOUND'); // Silly error code... + assert.strictEqual(err.hostname, 'nosuchhostimsure'); + + done(); + }); + + checkWrap(req); +}); + + +TEST(function test_resolve_failure(done) { + var req = dns.resolve4('nosuchhostimsure', function(err) { + assert(err instanceof Error); + assert.strictEqual(err.code, 'ENOTFOUND'); // Silly error code... + assert.strictEqual(err.hostname, 'nosuchhostimsure'); + + done(); + }); + + checkWrap(req); +}); + + /* Disabled because it appears to be not working on linux. */ /* TEST(function test_lookup_localhost_ipv6(done) { var req = dns.lookup('localhost', 6, function(err, ip, family) {