From baac81d95fa1f9d51b794ae6f4060e2d59a8a3c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Fri, 20 Nov 2015 22:16:55 +0100 Subject: [PATCH] net: add local address/port for better errors Adds localAddress and localPort to req so we have better error messages. Also fixes a case where ex is used before it is declared. PR-URL: https://github.com/nodejs/node/pull/3946 Reviewed-By: James M Snell Reviewed-By: Evan Lucas Reviewed-By: Colin Ihrig --- lib/net.js | 10 +++++++--- test/parallel/test-net-connect-local-error.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-net-connect-local-error.js diff --git a/lib/net.js b/lib/net.js index d7f3eae03914fc..f778043572362e 100644 --- a/lib/net.js +++ b/lib/net.js @@ -817,6 +817,8 @@ function connect(self, address, port, addressType, localAddress, localPort) { req.oncomplete = afterConnect; req.address = address; req.port = port; + req.localAddress = localAddress; + req.localPort = localPort; if (addressType === 4) err = self._handle.connect(req, address, port); @@ -1056,15 +1058,17 @@ function afterConnect(status, handle, req, readable, writable) { self._connecting = false; var details; if (req.localAddress && req.localPort) { - ex.localAddress = req.localAddress; - ex.localPort = req.localPort; - details = ex.localAddress + ':' + ex.localPort; + details = req.localAddress + ':' + req.localPort; } var ex = exceptionWithHostPort(status, 'connect', req.address, req.port, details); + if (details) { + ex.localAddress = req.localAddress; + ex.localPort = req.localPort; + } self._destroy(ex); } } diff --git a/test/parallel/test-net-connect-local-error.js b/test/parallel/test-net-connect-local-error.js new file mode 100644 index 00000000000000..197b283bdf3621 --- /dev/null +++ b/test/parallel/test-net-connect-local-error.js @@ -0,0 +1,15 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +var client = net.connect({ + port: common.PORT + 1, + localPort: common.PORT, + localAddress: common.localhostIPv4 +}); + +client.on('error', common.mustCall(function onError(err) { + assert.equal(err.localPort, common.PORT); + assert.equal(err.localAddress, common.localhostIPv4); +}));