diff --git a/lib/net.js b/lib/net.js index d50b3729d968a9..0091cd80b2ad11 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1469,8 +1469,10 @@ Object.defineProperty(Server.prototype, 'listening', { Server.prototype.address = function() { if (this._handle && this._handle.getsockname) { var out = {}; - this._handle.getsockname(out); - // TODO(bnoordhuis) Check err and throw? + var err = this._handle.getsockname(out); + if (err) { + throw errnoException(err, 'address'); + } return out; } else if (this._pipeName) { return this._pipeName; diff --git a/test/parallel/test-socket-address.js b/test/parallel/test-socket-address.js new file mode 100644 index 00000000000000..3e05f89f083223 --- /dev/null +++ b/test/parallel/test-socket-address.js @@ -0,0 +1,17 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +// This tests checks that if server._handle.getsockname +// returns an error number, an error is thrown. + +const server = net.createServer({}); +server.listen(0, common.mustCall(function() { + server._handle.getsockname = function(out) { + return -1; + }; + assert.throws(() => this.address(), + /^Error: address ([\w|\s-\d])+$/); + server.close(); +}));