diff --git a/lib/net.js b/lib/net.js index a5abd29379a..21e114f4109 100644 --- a/lib/net.js +++ b/lib/net.js @@ -27,6 +27,7 @@ var socketError = process.socketError; var getsockname = process.getsockname; var getaddrinfo = process.getaddrinfo; var needsLookup = process.needsLookup; +var errnoException = process.errnoException; var EINPROGRESS = process.EINPROGRESS; var ENOENT = process.ENOENT; var END_OF_FILE = 0; @@ -397,7 +398,7 @@ Socket.prototype.connect = function () { self._writeWatcher.set(self.fd, false, true); self._writeWatcher.start(); self._writeWatcher.callback = function () { - var errno = socketError(self.fd); + var errno = socketError(self.fd); if (errno == 0) { // connection established self._readWatcher.start(); @@ -406,9 +407,7 @@ Socket.prototype.connect = function () { self._writeWatcher.callback = self._doFlush; self.emit('connect'); } else if (errno != EINPROGRESS) { - var e = new Error('connection error'); - e.errno = errno; - self.forceClose(e); + self.forceClose(errnoException(errno, 'connect')); } }; } diff --git a/src/node_net2.cc b/src/node_net2.cc index 5e7cfef1798..560db1db980 100644 --- a/src/node_net2.cc +++ b/src/node_net2.cc @@ -1246,6 +1246,18 @@ static Handle NeedsLookup(const Arguments& args) { } +static Handle CreateErrnoException(const Arguments& args) { + HandleScope scope; + + int errorno = args[0]->Int32Value(); + String::Utf8Value syscall(args[1]->ToString()); + + Local exception = ErrnoException(errorno, *syscall); + + return scope.Close(exception); +} + + void InitNet2(Handle target) { HandleScope scope; @@ -1275,6 +1287,7 @@ void InitNet2(Handle target) { NODE_SET_METHOD(target, "getpeername", GetPeerName); NODE_SET_METHOD(target, "getaddrinfo", GetAddrInfo); NODE_SET_METHOD(target, "needsLookup", NeedsLookup); + NODE_SET_METHOD(target, "errnoException", CreateErrnoException); target->Set(String::NewSymbol("ENOENT"), Integer::New(ENOENT)); target->Set(String::NewSymbol("EINPROGRESS"), Integer::New(EINPROGRESS));