diff --git a/lib/net.js b/lib/net.js index 191d872c07e..41f2be08c6b 100644 --- a/lib/net.js +++ b/lib/net.js @@ -617,11 +617,15 @@ Stream.prototype.connect = function () { // TODO dns resolution on arguments[1] var port = arguments[0]; self._resolving = true; - lookupDomainName(arguments[1], function (ip, isV4) { - self.type = isV4 ? 'tcp4' : 'tcp6'; - self.fd = socket(self.type); - self._resolving = false; - doConnect(self, port, ip); + lookupDomainName(arguments[1], function (err, ip, isV4) { + if (err) { + self.emit('error', err); + } else { + self.type = isV4 ? 'tcp4' : 'tcp6'; + self.fd = socket(self.type); + self._resolving = false; + doConnect(self, port, ip); + } }); } else { self.fd = socket('unix'); @@ -761,7 +765,6 @@ exports.createServer = function (listener) { // callback(dn, isV4 function lookupDomainName (dn, callback) { var kind = isIP(dn); - debug('kind = ' + kind); if (kind) { // Always wait until the next tick this is so people can do // @@ -769,23 +772,28 @@ function lookupDomainName (dn, callback) { // server.addListener('listening', fn); // // Marginally slower, but a lot fewer WTFs. - process.nextTick(function () { callback(dn, kind == 4 ? true : false); }) + process.nextTick(function () { + callback(null, dn, kind == 4 ? true : false); + }); } else { debug("getaddrinfo 4 " + dn); - getaddrinfo(dn, 4, function (r4) { - if (r4 instanceof Error) throw r4; - if (r4.length > 0) { + getaddrinfo(dn, 4, function (err, r4) { + if (err) { + callback(err); + } else if (r4.length > 0) { debug("getaddrinfo 4 found " + r4); - callback(r4[0], true); + callback(null, r4[0], true); } else { debug("getaddrinfo 6 " + dn); - getaddrinfo(dn, 6, function (r6) { - if (r6 instanceof Error) throw r6; - if (r6.length < 0) { - throw new Error("No address associated with hostname " + dn); + getaddrinfo(dn, 6, function (err, r6) { + if (err) { + callback(err); + } else if (r6.length == 0) { + callback(new Error("No address associated with hostname " + dn)); + } else { + debug("getaddrinfo 6 found " + r6); + callback(null, r6[0], false); } - debug("getaddrinfo 6 found " + r6); - callback(r6[0], false); }); } }); @@ -847,11 +855,15 @@ Server.prototype.listen = function () { } else { // the first argument is the port, the second an IP var port = arguments[0]; - lookupDomainName(arguments[1], function (ip, isV4) { - self.type = isV4 ? 'tcp4' : 'tcp6'; - self.fd = socket(self.type); - bind(self.fd, port, ip); - self._doListen(); + lookupDomainName(arguments[1], function (err, ip, isV4) { + if (err) { + self.emit('error', err); + } else { + self.type = isV4 ? 'tcp4' : 'tcp6'; + self.fd = socket(self.type); + bind(self.fd, port, ip); + self._doListen(); + } }); } }; diff --git a/src/node_net2.cc b/src/node_net2.cc index de32dc419fd..2ba0f3f1ac1 100644 --- a/src/node_net2.cc +++ b/src/node_net2.cc @@ -1089,11 +1089,12 @@ static int AfterResolve(eio_req *req) { struct resolve_request * rreq = (struct resolve_request *)(req->data); HandleScope scope; - Local argv[1]; + Local argv[2]; if (req->result != 0) { + argv[1] = Array::New(); if (req->result == EAI_NODATA) { - argv[0] = Array::New(); + argv[0] = Local::New(Null()); } else { argv[0] = ErrnoException(req->result, "getaddrinfo", @@ -1127,12 +1128,13 @@ static int AfterResolve(eio_req *req) { address = address->ai_next; } - argv[0] = results; + argv[0] = Local::New(Null()); + argv[1] = results; } TryCatch try_catch; - rreq->cb->Call(Context::GetCurrent()->Global(), 1, argv); + rreq->cb->Call(Context::GetCurrent()->Global(), 2, argv); if (try_catch.HasCaught()) { FatalException(try_catch);