Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: add localAddress/Port for better error msgs #3946

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (req.localAddress && req.localPort) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at this point, you could change the conditional to be if (details) {

ex.localAddress = req.localAddress;
ex.localPort = req.localPort;
}
self._destroy(ex);
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-net-connect-local-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
var common = require('../common');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mind making these const instead of var?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied that from another test. Should I make a PR that changes this for every test where it isn't already the new way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the plan is to update them as we go. But in general, all new tests should use const wherever possible

var assert = require('assert');
var net = require('net');

var client = net.connect({
port: common.PORT + 1,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure which port to use here, it simply should produce an ECONNREFUSED error when connecting.

localPort: common.PORT,
localAddress: '127.0.0.1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hard coding the IP address, can you use common.localhostIPv4.

});

var onErrorCalled = false;
client.on('error', function(err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having the onErrorCalled, you could wrap the callback to the error event in common.mustCall().

You could then remove the process.on('exit') listener too

assert.equal(err.localPort, common.PORT);
assert.equal(err.localAddress, '127.0.0.1');
onErrorCalled = true;
});

process.on('exit', function() {
assert.ok(onErrorCalled);
});