Skip to content

Commit 951dbe4

Browse files
committed
dns: return real system error instead of 'ENOTFOUND'
Return the real system error to user insetad of injecting 'ENOTFOUND' which is not a proper POSIX error. nodejs#5099 (comment) Also fix the test suite to check for the corresponding error message. The hostname '...' was replaced with '***' because '...' returns inconsistent error message on different system. On GNU libc it intantly returns EAI_NONAME but on musl libc it returns EAI_AGAIN after a timeout.
1 parent f167207 commit 951dbe4

6 files changed

Lines changed: 12 additions & 19 deletions

lib/dns.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const net = require('net');
44
const util = require('util');
55

66
const cares = process.binding('cares_wrap');
7-
const uv = process.binding('uv');
87
const internalNet = require('internal/net');
98

109
const GetAddrInfoReqWrap = cares.GetAddrInfoReqWrap;
@@ -16,13 +15,6 @@ const isLegalPort = internalNet.isLegalPort;
1615

1716

1817
function errnoException(err, syscall, hostname) {
19-
// FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass
20-
// the true error to the user. ENOTFOUND is not even a proper POSIX error!
21-
if (err === uv.UV_EAI_MEMORY ||
22-
err === uv.UV_EAI_NODATA ||
23-
err === uv.UV_EAI_NONAME) {
24-
err = 'ENOTFOUND';
25-
}
2618
var ex = null;
2719
if (typeof err === 'string') { // c-ares error code.
2820
ex = new Error(syscall + ' ' + err + (hostname ? ' ' + hostname : ''));

test/parallel/test-http-dns-error.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ function test(mod) {
2727
// Ensure that there is time to attach an error listener.
2828
var req1 = mod.get({host: host, port: 42}, do_not_call);
2929
req1.on('error', common.mustCall(function(err) {
30-
assert.equal(err.code, 'ENOTFOUND');
30+
assert.equal(err.code, 'EAI_NONAME');
3131
}));
3232
// http.get() called req1.end() for us
3333

3434
var req2 = mod.request({method: 'GET', host: host, port: 42}, do_not_call);
3535
req2.on('error', common.mustCall(function(err) {
36-
assert.equal(err.code, 'ENOTFOUND');
36+
assert.equal(err.code, 'EAI_NONAME');
3737
}));
3838
req2.end();
3939
}

test/parallel/test-net-better-error-messages-port-hostname.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ var common = require('../common');
33
var net = require('net');
44
var assert = require('assert');
55

6-
var c = net.createConnection(common.PORT, '...');
6+
var c = net.createConnection(common.PORT, '***');
77

88
c.on('connect', common.fail);
99

1010
c.on('error', common.mustCall(function(e) {
11-
assert.equal(e.code, 'ENOTFOUND');
11+
assert.equal(e.code, 'EAI_NONAME');
1212
assert.equal(e.port, common.PORT);
13-
assert.equal(e.hostname, '...');
13+
assert.equal(e.hostname, '***');
1414
}));

test/parallel/test-net-connect-immediate-finish.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ const common = require('../common');
33
const assert = require('assert');
44
const net = require('net');
55

6-
const client = net.connect({host: '...', port: common.PORT});
6+
const client = net.connect({host: '***', port: common.PORT});
77

88
client.once('error', common.mustCall(function(err) {
99
assert(err);
1010
assert.strictEqual(err.code, err.errno);
11-
assert.strictEqual(err.code, 'ENOTFOUND');
11+
assert.strictEqual(err.code, 'EAI_NONAME');
1212
assert.strictEqual(err.host, err.hostname);
13-
assert.strictEqual(err.host, '...');
13+
assert.strictEqual(err.host, '***');
1414
assert.strictEqual(err.syscall, 'getaddrinfo');
1515
}));
1616

test/parallel/test-net-connect-options-ipv6.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ function tryConnect() {
4040
server.close();
4141
});
4242
}).on('error', function(err) {
43-
if (err.syscall === 'getaddrinfo' && err.code === 'ENOTFOUND') {
43+
if (err.syscall === 'getaddrinfo'
44+
&& (err.code === 'EAI_NODATA' || err.code === 'EAI_NONAME')) {
4445
if (host !== 'localhost' || --localhostTries === 0)
4546
host = hosts[++hostIdx];
4647
if (host)

test/parallel/test-net-dns-error.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ function do_not_call() {
2020

2121
var socket = net.connect(42, host, do_not_call);
2222
socket.on('error', function(err) {
23-
assert.equal(err.code, 'ENOTFOUND');
23+
assert.equal(err.code, 'EAI_NONAME');
2424
actual_bad_connections++;
2525
});
2626
socket.on('lookup', function(err, ip, type) {
2727
assert(err instanceof Error);
28-
assert.equal(err.code, 'ENOTFOUND');
28+
assert.equal(err.code, 'EAI_NONAME');
2929
assert.equal(ip, undefined);
3030
assert.equal(type, undefined);
3131
});

0 commit comments

Comments
 (0)