Skip to content

Commit

Permalink
net: check and throw on error for getsockname
Browse files Browse the repository at this point in the history
This commit attempts fix a TODO in net.js:
TODO(bnoordhuis) Check err and throw?

PR-URL: #12871
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Santiago Gimeno <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
danbev committed May 9, 2017
1 parent 771568a commit cf980b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1473,8 +1473,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;
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-socket-address.js
Original file line number Diff line number Diff line change
@@ -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();
}));

0 comments on commit cf980b0

Please sign in to comment.