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: improve error message when no address #239

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ var createServerHandle = exports._createServerHandle =


Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
debug('listen2', address, port, addressType, backlog);
debug('listen2', address, port, addressType, backlog, fd);
var self = this;

// If there is not yet a handle, we need to create one and bind.
Expand All @@ -1135,6 +1135,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
debug('_listen2: create a handle');
var rval = createServerHandle(address, port, addressType, fd);
if (util.isNumber(rval)) {
address = address || '0.0.0.0';
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this work on machines that only have IPv6 configured?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cjihrig , the listen will fallback to IPv4, when IPv6 failed.

  if (address || port || isTCP) {
    debug('bind to ' + (address || 'anycast'));
    if (!address) {
      // Try binding to ipv6 first
      err = handle.bind6('::', port);
      if (err) {
        handle.close();
        // Fallback to ipv4
        return createServerHandle('0.0.0.0', port);
      }
    } else if (addressType === 6) {
      err = handle.bind6(address, port);
    } else {
      err = handle.bind(address, port);
    }
  }

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't notice before that this is just for formatting an exception. I don't know how I feel about setting the value artificially just for that, but I'm OK with the rest of the PR.

var error = exceptionWithHostPort(rval, 'listen', address, port);
process.nextTick(function() {
self.emit('error', error);
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-net-better-error-message-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var common = require('../common');
var assert = require('assert');
var net = require('net');

var server = net.createServer(assert.fail);
server.listen(1, assert.fail);
server.on('error', common.mustCall(function(e) {
assert.equal(e.address, '0.0.0.0');
assert.equal(e.port, 1);
assert.equal(e.syscall, 'listen');
assert.equal(e.message, 'listen EACCES 0.0.0.0:1');
}));