Skip to content

Commit a2d1cbe

Browse files
committed
dns: set hostname property on error object
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.
1 parent ceb8740 commit a2d1cbe

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

lib/dns.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,27 @@ var uv = process.binding('uv');
2828
var isIp = net.isIP;
2929

3030

31-
function errnoException(err, syscall) {
31+
function errnoException(err, syscall, hostname) {
3232
// FIXME(bnoordhuis) Remove this backwards compatibility shite and pass
3333
// the true error to the user. ENOTFOUND is not even a proper POSIX error!
3434
if (err === uv.UV_EAI_MEMORY ||
3535
err === uv.UV_EAI_NODATA ||
3636
err === uv.UV_EAI_NONAME) {
3737
err = 'ENOTFOUND';
3838
}
39+
var ex = null;
3940
if (typeof err === 'string') { // c-ares error code.
40-
var ex = new Error(syscall + ' ' + err);
41+
ex = new Error(syscall + ' ' + err);
4142
ex.code = err;
4243
ex.errno = err;
4344
ex.syscall = syscall;
44-
return ex;
45+
} else {
46+
ex = util._errnoException(err, syscall);
47+
}
48+
if (hostname) {
49+
ex.hostname = hostname;
4550
}
46-
return util._errnoException(err, syscall);
51+
return ex;
4752
}
4853

4954

@@ -83,7 +88,7 @@ function makeAsync(callback) {
8388

8489
function onlookup(err, addresses) {
8590
if (err) {
86-
return this.callback(errnoException(err, 'getaddrinfo'));
91+
return this.callback(errnoException(err, 'getaddrinfo', this.hostname));
8792
}
8893
if (this.family) {
8994
this.callback(null, addresses[0], this.family);
@@ -133,10 +138,11 @@ exports.lookup = function(hostname, family, callback) {
133138
var req = {
134139
callback: callback,
135140
family: family,
141+
hostname: hostname,
136142
oncomplete: onlookup
137143
};
138144
var err = cares.getaddrinfo(req, hostname, family);
139-
if (err) throw errnoException(err, 'getaddrinfo');
145+
if (err) throw errnoException(err, 'getaddrinfo', hostname);
140146

141147
callback.immediately = true;
142148
return req;
@@ -145,7 +151,7 @@ exports.lookup = function(hostname, family, callback) {
145151

146152
function onresolve(err, result) {
147153
if (err)
148-
this.callback(errnoException(err, this.bindingName));
154+
this.callback(errnoException(err, this.bindingName, this.hostname));
149155
else
150156
this.callback(null, result);
151157
}
@@ -159,6 +165,7 @@ function resolver(bindingName) {
159165
var req = {
160166
bindingName: bindingName,
161167
callback: callback,
168+
hostname: name,
162169
oncomplete: onresolve
163170
};
164171
var err = binding(req, name);

test/internet/test-dns.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,45 @@ TEST(function test_lookup_localhost_ipv4(done) {
393393
});
394394

395395

396+
TEST(function test_reverse_failure(done) {
397+
var req = dns.reverse('0.0.0.0', function(err) {
398+
assert(err instanceof Error);
399+
assert.strictEqual(err.code, 'ENOTFOUND'); // Silly error code...
400+
assert.strictEqual(err.hostname, '0.0.0.0');
401+
402+
done();
403+
});
404+
405+
checkWrap(req);
406+
});
407+
408+
409+
TEST(function test_lookup_failure(done) {
410+
var req = dns.lookup('nosuchhostimsure', function(err) {
411+
assert(err instanceof Error);
412+
assert.strictEqual(err.code, 'ENOTFOUND'); // Silly error code...
413+
assert.strictEqual(err.hostname, 'nosuchhostimsure');
414+
415+
done();
416+
});
417+
418+
checkWrap(req);
419+
});
420+
421+
422+
TEST(function test_resolve_failure(done) {
423+
var req = dns.resolve4('nosuchhostimsure', function(err) {
424+
assert(err instanceof Error);
425+
assert.strictEqual(err.code, 'ENOTFOUND'); // Silly error code...
426+
assert.strictEqual(err.hostname, 'nosuchhostimsure');
427+
428+
done();
429+
});
430+
431+
checkWrap(req);
432+
});
433+
434+
396435
/* Disabled because it appears to be not working on linux. */
397436
/* TEST(function test_lookup_localhost_ipv6(done) {
398437
var req = dns.lookup('localhost', 6, function(err, ip, family) {

0 commit comments

Comments
 (0)