Skip to content

Commit

Permalink
net: add local address/port for better errors
Browse files Browse the repository at this point in the history
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: #3946
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
jscissr authored and evanlucas committed Nov 24, 2015
1 parent 5169311 commit d7b199d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1058,15 +1060,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);
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-net-connect-local-error.js
Original file line number Diff line number Diff line change
@@ -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);
}));

0 comments on commit d7b199d

Please sign in to comment.