dns: set hostname property on error object
authorBen Noordhuis <info@bnoordhuis.nl>
Tue, 15 Oct 2013 11:01:23 +0000 (13:01 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Wed, 16 Oct 2013 19:56:16 +0000 (21:56 +0200)
Make debugging and logging easier: when a DNS lookup for a hostname
fails, set the hostname as a property on the error object.

Fixes #5393.

lib/dns.js
test/internet/test-dns.js

index 9ced633..04cf013 100644 (file)
@@ -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);
+  }
+  if (hostname) {
+    ex.hostname = hostname;
   }
-  return util._errnoException(err, syscall);
+  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);
index 5f360b4..cc69106 100644 (file)
@@ -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) {