diff --git a/doc/api/errors.md b/doc/api/errors.md
index 6ffbfe4c95f78b..723a33559dc0ca 100644
--- a/doc/api/errors.md
+++ b/doc/api/errors.md
@@ -243,7 +243,7 @@ new MyError().stack;
### Error.stackTraceLimit
-* {Number}
+* {number}
The `Error.stackTraceLimit` property specifies the number of stack frames
collected by a stack trace (whether generated by `new Error().stack` or
@@ -255,9 +255,16 @@ will affect any stack trace captured *after* the value has been changed.
If set to a non-number value, or set to a negative number, stack traces will
not capture any frames.
-### error.message
+#### error.code
+
+* {string}
+
+The `error.code` property is a string label that identifies the kind of error.
+See [Node.js Error Codes][] for details about specific codes.
-* {String}
+#### error.message
+
+* {string}
The `error.message` property is the string description of the error as set by calling `new Error(message)`.
The `message` passed to the constructor will also appear in the first line of
@@ -273,7 +280,7 @@ console.error(err.message);
### error.stack
-* {String}
+* {string}
The `error.stack` property is a string describing the point in the code at which
the `Error` was instantiated.
@@ -449,14 +456,14 @@ added properties.
#### error.code
-* {String}
+* {string}
The `error.code` property is a string representing the error code, which is always
`E` followed by a sequence of capital letters.
#### error.errno
-* {String | Number}
+* {string | number}
The `error.errno` property is a number or a string.
The number is a **negative** value which corresponds to the error code defined in
@@ -466,27 +473,27 @@ In case of a string, it is the same as `error.code`.
#### error.syscall
-* {String}
+* {string}
The `error.syscall` property is a string describing the [syscall][] that failed.
#### error.path
-* {String}
+* {string}
When present (e.g. in `fs` or `child_process`), the `error.path` property is a string
containing a relevant invalid pathname.
#### error.address
-* {String}
+* {string}
When present (e.g. in `net` or `dgram`), the `error.address` property is a string
describing the address to which the connection failed.
#### error.port
-* {Number}
+* {number}
When present (e.g. in `net` or `dgram`), the `error.port` property is a number representing
the connection's port that is not available.
@@ -550,6 +557,58 @@ found [here][online].
encountered by [`http`][] or [`net`][] -- often a sign that a `socket.end()`
was not properly called.
+
+## Node.js Error Codes
+
+
+### ERR_INVALID_ARGS
+
+The `'ERR_INVALID_ARGS'` error code is used generically to identify that
+one or more arguments were passed wrong to a Node.js API.
+
+
+### ERR_INVALID_ARG_TYPE
+
+The `'ERR_INVALID_ARG_TYPE'` error code is used generically to identify that
+an argument of the wrong type has been passed to a Node.js API.
+
+
+### ERR_INVALID_ARG_VALUE
+
+The `'ERR_INVALID_ARG_VALUE'` error code is used generically to identify that
+an argument containing the wrong value was passed to a Node.js API.
+
+
+### ERR_INVALID_CALLBACK
+
+The `'ERR_INVALID_CALLBACK'` error code is used generically to identify that
+a callback function is required and has not been provided to a Node.js API.
+
+
+### ERR_INVALID_FLAG
+
+The `'ERR_INVALID_FLAG'` error code is used generically to identify that
+a invalid flag was passed as an argument to a Node.js API.
+
+
+### ERR_INVALID_IP
+
+The `'ERR_INVALID_IP'` error code is used generically to identify that
+a invalid value was passed as an IP to a Node.js API.
+
+
+### ERR_INVALID_PORT
+
+The `'ERR_INVALID_PORT'` error code is used generically to identify that
+a invalid value was passed as a PORT to a Node.js API.
+
+
+### ERR_SETTING_SERVERS
+
+The `'ERR_SETTING_SERVERS'` error code is used generically to identify that
+an error occured setting the DNS servers
+
+
[`fs.readdir`]: fs.html#fs_fs_readdir_path_options_callback
[`fs.readFileSync`]: fs.html#fs_fs_readfilesync_file_options
[`fs.unlink`]: fs.html#fs_fs_unlink_path_callback
@@ -562,9 +621,10 @@ found [here][online].
[domains]: domain.html
[event emitter-based]: events.html#events_class_eventemitter
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
+[Node.js Error Codes]: #nodejs-error-codes
[online]: http://man7.org/linux/man-pages/man3/errno.3.html
[stream-based]: stream.html
[syscall]: http://man7.org/linux/man-pages/man2/syscall.2.html
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
[V8's stack trace API]: https://github.com/v8/v8/wiki/Stack-Trace-API
-[vm]: vm.html
+[vm]: vm.html
\ No newline at end of file
diff --git a/lib/dns.js b/lib/dns.js
index 1f22c91c78c27a..c35bc97a4cb43c 100644
--- a/lib/dns.js
+++ b/lib/dns.js
@@ -5,6 +5,7 @@ const util = require('util');
const cares = process.binding('cares_wrap');
const uv = process.binding('uv');
const internalNet = require('internal/net');
+const errors = require('internal/errors');
const GetAddrInfoReqWrap = cares.GetAddrInfoReqWrap;
const GetNameInfoReqWrap = cares.GetNameInfoReqWrap;
@@ -18,8 +19,8 @@ function errnoException(err, syscall, hostname) {
// FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass
// the true error to the user. ENOTFOUND is not even a proper POSIX error!
if (err === uv.UV_EAI_MEMORY ||
- err === uv.UV_EAI_NODATA ||
- err === uv.UV_EAI_NONAME) {
+ err === uv.UV_EAI_NODATA ||
+ err === uv.UV_EAI_NONAME) {
err = 'ENOTFOUND';
}
var ex = null;
@@ -106,30 +107,32 @@ function lookup(hostname, options, callback) {
// Parse arguments
if (hostname && typeof hostname !== 'string') {
- throw new TypeError('Invalid arguments: ' +
- 'hostname must be a string or falsey');
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
+ 'hostname', ['string', 'buffer']);
} else if (typeof options === 'function') {
callback = options;
family = 0;
} else if (typeof callback !== 'function') {
- throw new TypeError('Invalid arguments: callback must be passed');
+ // throw new TypeError('Invalid arguments: callback must be passed');
+ throw new errors.TypeError('ERR_INVALID_CALLBACK');
} else if (options !== null && typeof options === 'object') {
hints = options.hints >>> 0;
family = options.family >>> 0;
all = options.all === true;
if (hints !== 0 &&
- hints !== cares.AI_ADDRCONFIG &&
- hints !== cares.AI_V4MAPPED &&
- hints !== (cares.AI_ADDRCONFIG | cares.AI_V4MAPPED)) {
- throw new TypeError('Invalid argument: hints must use valid flags');
+ hints !== cares.AI_ADDRCONFIG &&
+ hints !== cares.AI_V4MAPPED &&
+ hints !== (cares.AI_ADDRCONFIG | cares.AI_V4MAPPED)) {
+ throw new errors.Error('ERR_INVALID_FLAG', 'hints');
}
} else {
family = options >>> 0;
}
if (family !== 0 && family !== 4 && family !== 6)
- throw new TypeError('Invalid argument: family must be 4 or 6');
+ throw new errors.Error('ERR_INVALID_ARG_VALUE',
+ 'family', ['4', '6']);
callback = makeAsync(callback);
@@ -180,16 +183,16 @@ function onlookupservice(err, host, service) {
// lookupService(address, port, callback)
function lookupService(host, port, callback) {
if (arguments.length !== 3)
- throw new Error('Invalid arguments');
+ throw new errors.Error('ERR_INVALID_ARGS');
if (isIP(host) === 0)
- throw new TypeError('"host" argument needs to be a valid IP address');
+ throw new errors.Error('ERR_INVALID_IP', 'host', host);
if (!isLegalPort(port))
- throw new TypeError(`"port" should be >= 0 and < 65536, got "${port}"`);
+ throw new errors.RangeError('ERR_INVALID_PORT', 'port', port);
if (typeof callback !== 'function')
- throw new TypeError('"callback" argument must be a function');
+ throw new errors.TypeError('ERR_INVALID_CALLBACK');
port = +port;
callback = makeAsync(callback);
@@ -230,9 +233,9 @@ function resolver(bindingName) {
}
if (typeof name !== 'string') {
- throw new Error('"name" argument must be a string');
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
} else if (typeof callback !== 'function') {
- throw new Error('"callback" argument must be a function');
+ throw new errors.TypeError('ERR_INVALID_CALLBACK');
}
callback = makeAsync(callback);
@@ -272,13 +275,15 @@ function resolve(hostname, type_, callback_) {
resolver = resolveMap.A;
callback = type_;
} else {
- throw new Error('"type" argument must be a string');
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
+ 'type', ['string', 'function']);
}
if (typeof resolver === 'function') {
return resolver(hostname, callback);
} else {
- throw new Error(`Unknown type "${type_}"`);
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
+ 'resolver', 'function');
}
}
@@ -313,7 +318,7 @@ function setServers(servers) {
if (ipVersion !== 0)
return newSet.push([ipVersion, s]);
- throw new Error(`IP address is not properly formatted: ${serv}`);
+ throw new errors.Error('ERR_INVALID_IP', 'serv', serv);
});
const errorNumber = cares.setServers(newSet);
@@ -323,7 +328,7 @@ function setServers(servers) {
cares.setServers(orig.join(','));
var err = cares.strerror(errorNumber);
- throw new Error(`c-ares failed to set servers: "${err}" [${servers}]`);
+ throw new errors.Error('ERR_SETTINGS_SERVERS', err, servers);
}
}
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index f2376f70371c60..7baf68963c1b36 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -85,4 +85,63 @@ module.exports = exports = {
//
// Note: Please try to keep these in alphabetical order
E('ERR_ASSERTION', (msg) => msg);
+E('ERR_INVALID_ARGS', 'invalid arguments');
+E('ERR_INVALID_ARG_TYPE', invalidArgType);
+E('ERR_INVALID_ARG_VALUE', invalidArgValue);
+E('ERR_INVALID_CALLBACK', 'callback must be a function');
+E('ERR_INVALID_FLAG', (arg) => `${arg} must use valid flags`);
+E('ERR_INVALID_IP',
+ (arg, ip) => `"${arg}" argument must be a valid IP address, got ${ip}`);
+E('ERR_INVALID_PORT',
+ (arg, port) => `"${arg}" argument must be >= 0 and < 65536, got ${port}`);
+E('ERR_SETTING_SERVERS',
+ (err, servers) => `c-ares failed to set servers: "${err}" [${servers}]`);
// Add new errors from here...
+
+// Errors from 111294, port error from 11302
+
+function invalidArgType(name, expected, actual) {
+ const assert = lazyAssert();
+ assert(name, 'name is required');
+ assert(expected, 'expected is required');
+ var msg = `The "${name}" argument must be `;
+ if (Array.isArray(expected)) {
+ var len = expected.length;
+ expected = expected.map((i) => String(i));
+ if (len > 1) {
+ msg += `one of type ${expected.slice(0, len - 1).join(', ')}, or ` +
+ expected[len - 1];
+ } else {
+ msg += `type ${expected[0]}`;
+ }
+ } else {
+ msg += `type ${String(expected)}`;
+ }
+ if (arguments.length >= 3) {
+ msg += `. Received type ${actual !== null ? typeof actual : 'null'}`;
+ }
+ return msg;
+}
+
+function invalidArgValue(name, expected, actual) {
+ const assert = lazyAssert();
+ assert(name, 'name is required');
+ assert(expected, 'expected is required');
+ var msg = `The "${name}" argument must be `;
+ if (Array.isArray(expected)) {
+ var len = expected.length;
+ expected = expected.map((i) => String(i));
+ if (len > 1) {
+ msg += `${expected.slice(0, len - 1).join(', ')}, or ` +
+ expected[len - 1];
+ } else {
+ msg += `${expected[0]}`;
+ }
+ } else {
+ msg += `${String(expected)}`;
+ }
+ if (arguments.length >= 3) {
+ msg += `. Received ${actual !== null ? typeof actual : 'null'}`;
+ }
+ return msg;
+}
diff --git a/lib/internal/net.js b/lib/internal/net.js
index 8f279cad1608f5..ff9fc3657b36bb 100644
--- a/lib/internal/net.js
+++ b/lib/internal/net.js
@@ -1,5 +1,7 @@
'use strict';
+const errors = require('internal/errors');
+
// Check that the port number is not NaN when coerced to a number,
// is an integer and that it falls within the legal range of port numbers.
function isLegalPort(port) {
@@ -12,7 +14,7 @@ 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('ERR_INVALID_PORT', 'port', port);
}
module.exports = {
diff --git a/lib/net.js b/lib/net.js
index d498efb184e713..dc54b15325d868 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -9,6 +9,7 @@ const internalNet = require('internal/net');
const assert = require('assert');
const cares = process.binding('cares_wrap');
const uv = process.binding('uv');
+const errors = require('internal/errors');
const Buffer = require('buffer').Buffer;
const TTYWrap = process.binding('tty_wrap');
@@ -32,7 +33,7 @@ function createHandle(fd) {
var type = TTYWrap.guessHandleType(fd);
if (type === 'PIPE') return new Pipe();
if (type === 'TCP') return new TCP();
- throw new TypeError('Unsupported fd type: ' + type);
+ throw new errors.TypeError('ERR_INVALID_FLAG', 'hints');
}
@@ -654,8 +655,8 @@ protoGetter('localPort', function localPort() {
Socket.prototype.write = function(chunk, encoding, cb) {
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
- throw new TypeError(
- 'Invalid data, chunk must be a string or buffer, not ' + typeof chunk);
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'chunk',
+ ['string', 'buffer']);
}
return stream.Duplex.prototype.write.apply(this, arguments);
};
@@ -949,18 +950,17 @@ function lookupAndConnect(self, options) {
var localPort = options.localPort;
if (localAddress && !exports.isIP(localAddress))
- throw new TypeError('"localAddress" option must be a valid IP: ' +
- localAddress);
+ throw new errors.TypeError('ERR_INVALID_IP', 'localAddress', localAddress);
if (localPort && typeof localPort !== 'number')
- throw new TypeError('"localPort" option should be a number: ' + localPort);
+ throw new errors.RangeError('ERR_INVALID_PORT', 'localPort', localPort);
if (typeof port !== 'undefined') {
if (typeof port !== 'number' && typeof port !== 'string')
- throw new TypeError('"port" option should be a number or string: ' +
- port);
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'port',
+ ['number', 'string']);
if (!isLegalPort(port))
- throw new RangeError('"port" option should be >= 0 and < 65536: ' + port);
+ throw new errors.RangeError('ERR_INVALID_PORT', 'port', port);
}
port |= 0;
@@ -975,7 +975,8 @@ function lookupAndConnect(self, options) {
}
if (options.lookup && typeof options.lookup !== 'function')
- throw new TypeError('"lookup" option should be a function');
+ // throw new TypeError('"lookup" option should be a function');
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'lookup', 'function');
var dnsopts = {
family: options.family,
@@ -1117,7 +1118,7 @@ function Server(options, connectionListener) {
this.on('connection', connectionListener);
}
} else {
- throw new TypeError('options must be an object');
+ throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
}
this._connections = 0;
@@ -1380,7 +1381,7 @@ Server.prototype.listen = function() {
const pipeName = this._pipeName = options.path;
listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive);
} else {
- throw new Error('Invalid listen argument: ' + options);
+ throw new errors.Error('ERR_INVALID_ARGS');
}
}
diff --git a/test/parallel/test-c-ares.js b/test/parallel/test-c-ares.js
index 2eb824b5df28e9..1e8f01e71e97d4 100644
--- a/test/parallel/test-c-ares.js
+++ b/test/parallel/test-c-ares.js
@@ -26,10 +26,14 @@ dns.lookup('::1', common.mustCall((error, result, addressType) => {
}));
// Try calling resolve with an unsupported type.
-assert.throws(() => dns.resolve('www.google.com', 'HI'), /Unknown type/);
+assert.throws(() => dns.resolve('www.google.com', 'HI'),
+ common.expectsError({code: 'ERR_INVALID_ARG_TYPE',
+ type: TypeError}));
// Try calling resolve with an unsupported type that's an object key
-assert.throws(() => dns.resolve('www.google.com', 'toString'), /Unknown type/);
+assert.throws(() => dns.resolve('www.google.com', 'toString'),
+ common.expectsError({code: 'ERR_INVALID_ARG_TYPE',
+ type: TypeError}));
// Windows doesn't usually have an entry for localhost 127.0.0.1 in
// C:\Windows\System32\drivers\etc\hosts
diff --git a/test/parallel/test-dns-lookup.js b/test/parallel/test-dns-lookup.js
index a720c46e02a630..5ad5556cd522d9 100644
--- a/test/parallel/test-dns-lookup.js
+++ b/test/parallel/test-dns-lookup.js
@@ -7,17 +7,17 @@ const dns = require('dns');
// Stub `getaddrinfo` to *always* error.
cares.getaddrinfo = () => process.binding('uv').UV_ENOENT;
-assert.throws(() => {
- dns.lookup(1, {});
-}, /^TypeError: Invalid arguments: hostname must be a string or falsey$/);
+assert.throws(() => dns.lookup(1, {}),
+ common.expectsError({code: 'ERR_INVALID_ARG_TYPE',
+ type: TypeError}));
-assert.throws(() => {
- dns.lookup(false, 'cb');
-}, /^TypeError: Invalid arguments: callback must be passed$/);
+assert.throws(() => dns.lookup(false, 'cb'),
+ common.expectsError({code: 'ERR_INVALID_CALLBACK',
+ type: TypeError}));
-assert.throws(() => {
- dns.lookup(false, 'options', 'cb');
-}, /^TypeError: Invalid arguments: callback must be passed$/);
+assert.throws(() => dns.lookup(false, 'options', 'cb'),
+ common.expectsError({code: 'ERR_INVALID_CALLBACK',
+ type: TypeError}));
assert.throws(() => {
dns.lookup(false, {
@@ -25,7 +25,7 @@ assert.throws(() => {
family: 0,
all: false
}, () => {});
-}, /^TypeError: Invalid argument: hints must use valid flags$/);
+}, common.expectsError({code: 'ERR_INVALID_FLAG', type: Error}));
assert.throws(() => {
dns.lookup(false, {
@@ -33,7 +33,7 @@ assert.throws(() => {
family: 20,
all: false
}, () => {});
-}, /^TypeError: Invalid argument: family must be 4 or 6$/);
+}, common.expectsError({code: 'ERR_INVALID_ARG_VALUE', type: Error}));
assert.doesNotThrow(() => {
dns.lookup(false, {
diff --git a/test/parallel/test-dns-regress-7070.js b/test/parallel/test-dns-regress-7070.js
index eb939b47559a9d..fb638e0f95661f 100644
--- a/test/parallel/test-dns-regress-7070.js
+++ b/test/parallel/test-dns-regress-7070.js
@@ -1,10 +1,13 @@
'use strict';
require('../common');
+const common = require('../common');
const assert = require('assert');
const dns = require('dns');
// Should not raise assertion error. Issue #7070
assert.throws(() => dns.resolveNs([]), // bad name
- /^Error: "name" argument must be a string$/);
+ common.expectsError({code: 'ERR_INVALID_ARG_TYPE',
+ type: TypeError}));
assert.throws(() => dns.resolveNs(''), // bad callback
- /^Error: "callback" argument must be a function$/);
+ common.expectsError({code: 'ERR_INVALID_CALLBACK',
+ type: TypeError}));
diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js
index 27b9b81d7d14a2..bde25fbd3d8718 100644
--- a/test/parallel/test-dns.js
+++ b/test/parallel/test-dns.js
@@ -1,5 +1,6 @@
'use strict';
require('../common');
+const common = require('../common');
const assert = require('assert');
const dns = require('dns');
@@ -43,7 +44,7 @@ const goog = [
assert.doesNotThrow(() => dns.setServers(goog));
assert.deepStrictEqual(dns.getServers(), goog);
assert.throws(() => dns.setServers(['foobar']),
- /^Error: IP address is not properly formatted: foobar$/);
+ common.expectsError({code: 'ERR_INVALID_IP', type: Error}));
assert.deepStrictEqual(dns.getServers(), goog);
const goog6 = [
@@ -71,15 +72,12 @@ assert.deepStrictEqual(dns.getServers(), portsExpected);
assert.doesNotThrow(() => dns.setServers([]));
assert.deepStrictEqual(dns.getServers(), []);
-assert.throws(() => {
- dns.resolve('test.com', [], noop);
-}, function(err) {
- return !(err instanceof TypeError);
-}, 'Unexpected error');
+assert.throws(() => dns.resolve('test.com', [], noop),
+ common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: Error}));
// dns.lookup should accept falsey and string values
const errorReg =
- /^TypeError: Invalid arguments: hostname must be a string or falsey$/;
+ common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: TypeError});
assert.throws(() => dns.lookup({}, noop), errorReg);
@@ -113,13 +111,15 @@ assert.doesNotThrow(() => dns.lookup(NaN, noop));
assert.throws(() => {
dns.lookup('www.google.com', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
noop);
-}, /^TypeError: Invalid argument: hints must use valid flags$/);
+}, common.expectsError({code: 'ERR_INVALID_FLAG', type: Error}));
assert.throws(() => dns.lookup('www.google.com'),
- /^TypeError: Invalid arguments: callback must be passed$/);
+ common.expectsError({code: 'ERR_INVALID_CALLBACK',
+ type: TypeError}));
assert.throws(() => dns.lookup('www.google.com', 4),
- /^TypeError: Invalid arguments: callback must be passed$/);
+ common.expectsError({code: 'ERR_INVALID_CALLBACK',
+ type: TypeError}));
assert.doesNotThrow(() => dns.lookup('www.google.com', 6, noop));
@@ -143,28 +143,32 @@ assert.doesNotThrow(() => {
});
assert.throws(() => dns.lookupService('0.0.0.0'),
- /^Error: Invalid arguments$/);
+ common.expectsError({code: 'ERR_INVALID_ARGS', type: Error}));
assert.throws(() => dns.lookupService('fasdfdsaf', 0, noop),
- /^TypeError: "host" argument needs to be a valid IP address$/);
+ common.expectsError({code: 'ERR_INVALID_IP', type: Error}));
assert.doesNotThrow(() => dns.lookupService('0.0.0.0', '0', noop));
assert.doesNotThrow(() => dns.lookupService('0.0.0.0', 0, noop));
assert.throws(() => dns.lookupService('0.0.0.0', null, noop),
- /^TypeError: "port" should be >= 0 and < 65536, got "null"$/);
+ common.expectsError({code: 'ERR_INVALID_PORT',
+ type: RangeError}));
assert.throws(
() => dns.lookupService('0.0.0.0', undefined, noop),
- /^TypeError: "port" should be >= 0 and < 65536, got "undefined"$/
+ common.expectsError({code: 'ERR_INVALID_PORT', type: RangeError})
);
assert.throws(() => dns.lookupService('0.0.0.0', 65538, noop),
- /^TypeError: "port" should be >= 0 and < 65536, got "65538"$/);
+ common.expectsError({code: 'ERR_INVALID_PORT',
+ type: RangeError}));
assert.throws(() => dns.lookupService('0.0.0.0', 'test', noop),
- /^TypeError: "port" should be >= 0 and < 65536, got "test"$/);
+ common.expectsError({code: 'ERR_INVALID_PORT',
+ type: RangeError}));
assert.throws(() => dns.lookupService('0.0.0.0', 80, null),
- /^TypeError: "callback" argument must be a function$/);
+ common.expectsError({code: 'ERR_INVALID_CALLBACK',
+ type: Error}));
diff --git a/test/parallel/test-net-create-connection.js b/test/parallel/test-net-create-connection.js
index dc5981ecd20221..2df8f707bd6435 100644
--- a/test/parallel/test-net-create-connection.js
+++ b/test/parallel/test-net-create-connection.js
@@ -21,11 +21,8 @@ server.listen(0, 'localhost', function() {
}
function fail(opts, errtype, msg) {
- assert.throws(function() {
- net.createConnection(opts, cb);
- }, function(err) {
- return err instanceof errtype && msg === err.message;
- });
+ assert.throws(() => net.createConnection(opts, cb),
+ (err) => err instanceof errtype && msg === err.code);
}
net.createConnection(this.address().port).on('connect', cb);
@@ -38,59 +35,59 @@ server.listen(0, 'localhost', function() {
fail({
port: true
- }, TypeError, '"port" option should be a number or string: true');
+ }, TypeError, 'ERR_INVALID_ARG_TYPE');
fail({
port: false
- }, TypeError, '"port" option should be a number or string: false');
+ }, TypeError, 'ERR_INVALID_ARG_TYPE');
fail({
port: []
- }, TypeError, '"port" option should be a number or string: ');
+ }, TypeError, 'ERR_INVALID_ARG_TYPE');
fail({
port: {}
- }, TypeError, '"port" option should be a number or string: [object Object]');
+ }, TypeError, 'ERR_INVALID_ARG_TYPE');
fail({
port: null
- }, TypeError, '"port" option should be a number or string: null');
+ }, TypeError, 'ERR_INVALID_ARG_TYPE');
fail({
port: ''
- }, RangeError, '"port" option should be >= 0 and < 65536: ');
+ }, RangeError, 'ERR_INVALID_PORT');
fail({
port: ' '
- }, RangeError, '"port" option should be >= 0 and < 65536: ');
+ }, RangeError, 'ERR_INVALID_PORT');
fail({
port: '0x'
- }, RangeError, '"port" option should be >= 0 and < 65536: 0x');
+ }, RangeError, 'ERR_INVALID_PORT');
fail({
port: '-0x1'
- }, RangeError, '"port" option should be >= 0 and < 65536: -0x1');
+ }, RangeError, 'ERR_INVALID_PORT');
fail({
port: NaN
- }, RangeError, '"port" option should be >= 0 and < 65536: NaN');
+ }, RangeError, 'ERR_INVALID_PORT');
fail({
port: Infinity
- }, RangeError, '"port" option should be >= 0 and < 65536: Infinity');
+ }, RangeError, 'ERR_INVALID_PORT');
fail({
port: -1
- }, RangeError, '"port" option should be >= 0 and < 65536: -1');
+ }, RangeError, 'ERR_INVALID_PORT');
fail({
port: 65536
- }, RangeError, '"port" option should be >= 0 and < 65536: 65536');
+ }, RangeError, 'ERR_INVALID_PORT');
fail({
hints: (dns.ADDRCONFIG | dns.V4MAPPED) + 42,
- }, TypeError, 'Invalid argument: hints must use valid flags');
+ }, Error, 'ERR_INVALID_FLAG');
});
// Try connecting to random ports, but do so once the server is closed
diff --git a/test/parallel/test-net-listen-port-option.js b/test/parallel/test-net-listen-port-option.js
index 693bf97907aeee..fca96a3bd938dc 100644
--- a/test/parallel/test-net-listen-port-option.js
+++ b/test/parallel/test-net-listen-port-option.js
@@ -36,10 +36,12 @@ listenVariants.forEach((listenVariant, i) => {
return;
}
assert.throws(() => listenVariant(port, common.mustNotCall()),
- /"port" argument must be >= 0 and < 65536/i);
+ common.expectsError({code: 'ERR_INVALID_PORT',
+ type: RangeError}));
});
[null, true, false].forEach((port) =>
assert.throws(() => listenVariant(port, common.mustNotCall()),
- /invalid listen argument/i));
+ common.expectsError({code: 'ERR_INVALID_ARGS',
+ type: Error})));
});
diff --git a/test/parallel/test-net-localerror.js b/test/parallel/test-net-localerror.js
index 184e55c890b2bb..0818db1f4d986d 100644
--- a/test/parallel/test-net-localerror.js
+++ b/test/parallel/test-net-localerror.js
@@ -7,13 +7,13 @@ connect({
host: 'localhost',
port: common.PORT,
localPort: 'foobar',
-}, /^TypeError: "localPort" option should be a number: foobar$/);
+}, common.expectsError({code: 'ERR_INVALID_PORT', type: RangeError}));
connect({
host: 'localhost',
port: common.PORT,
localAddress: 'foobar',
-}, /^TypeError: "localAddress" option must be a valid IP: foobar$/);
+}, common.expectsError({code: 'ERR_INVALID_IP', type: Error}));
function connect(opts, msg) {
assert.throws(function() {
diff --git a/test/parallel/test-net-socket-write-error.js b/test/parallel/test-net-socket-write-error.js
index 2af04d310903be..821bf159c7698c 100644
--- a/test/parallel/test-net-socket-write-error.js
+++ b/test/parallel/test-net-socket-write-error.js
@@ -1,6 +1,7 @@
'use strict';
require('../common');
+const common = require('../common');
const assert = require('assert');
const net = require('net');
@@ -10,7 +11,7 @@ function connectToServer() {
const client = net.createConnection(this.address().port, () => {
assert.throws(() => {
client.write(1337);
- }, /Invalid data, chunk must be a string or buffer, not number/);
+ }, common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: Error}));
client.end();
})