Skip to content
Prev Previous commit
Next Next commit
errors: update internal/net.js to use internal/errors
  • Loading branch information
jasnell committed Oct 25, 2016
commit c9b2093e3d059f80afc68c8b820201e189063225
7 changes: 4 additions & 3 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NodeError extends Error {
args[n - 1] = arguments[n];
super(message(key, args));
this[kCode] = key;
Error.captureStackTrace(this, new.target);
Error.captureStackTrace(this, NodeError);
}

get name() {
Expand All @@ -38,7 +38,7 @@ class NodeTypeError extends TypeError {
args[n - 1] = arguments[n];
super(message(key, args));
this[kCode] = key;
Error.captureStackTrace(this, new.target);
Error.captureStackTrace(this, NodeTypeError);
}

get name() {
Expand All @@ -57,7 +57,7 @@ class NodeRangeError extends RangeError {
args[n - 1] = arguments[n];
super(message(key, args));
this[kCode] = key;
Error.captureStackTrace(this, new.target);
Error.captureStackTrace(this, NodeRangeError);
}

get name() {
Expand Down Expand Up @@ -118,6 +118,7 @@ function E(sym, val) {

// Note: Please try to keep these in alphabetical order
E('ASSERTION_ERROR', (msg) => msg);
E('INVALID_PORT', 'port must be a number >= 0 and <= 65535');
E('NATIVE_MODULE_NOT_FOUND', (module) => `No such native module ${module}`);
E('NO_CRYPTO', 'Node.js is not compiled with openssl crypto support');
E('SOCKET_CLOSED_BEFORE_REPLY', 'Socket closed before reply');
3 changes: 2 additions & 1 deletion lib/internal/net.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const errors = require('internal/errors');
module.exports = { isLegalPort, assertPort };

// Check that the port number is not NaN when coerced to a number,
Expand All @@ -14,5 +15,5 @@ function isLegalPort(port) {

function assertPort(port) {
if (typeof port !== 'undefined' && !isLegalPort(port))
throw new RangeError('"port" argument must be >= 0 and < 65536');
throw new errors.RangeError('INVALID_PORT');
}
7 changes: 5 additions & 2 deletions test/parallel/test-net-listen-port-option.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const errors = require('internal/errors');
const assert = require('assert');
const net = require('net');

const invalid_port = new RegExp(`${errors.message('INVALID_PORT')}`);

function close() { this.close(); }

// From lib/net.js
Expand Down Expand Up @@ -35,8 +39,7 @@ listenVariants.forEach((listenVariant, i) => {
// skip this, because listen(port) can also be listen(path)
return;
}
assert.throws(() => listenVariant(port, common.fail),
/"port" argument must be >= 0 and < 65536/i);
assert.throws(() => listenVariant(port, common.fail), invalid_port);
});

[null, true, false].forEach((port) =>
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-regress-GH-5727.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const errors = require('internal/errors');

const invalidPort = -1 >>> 0;
const errorMessage = /"port" argument must be >= 0 and < 65536/;
const errorMessage = new RegExp(`${errors.message('INVALID_PORT')}`);

net.Server().listen(common.PORT, function() {
const address = this.address();
Expand Down