lib: always show ERR_INVALID_ARG_TYPE received part

This makes a effort to make sure all of these errors will actually
also show the received input.
On top of that it refactors a few tests for better maintainability.
It will also change the returned type to always be a simple typeof
instead of special handling null.

PR-URL: https://github.com/nodejs/node/pull/19445
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Ruben Bridgewater 2018-03-19 13:33:46 +01:00
parent eeb57022e6
commit c6b6c92185
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
107 changed files with 746 additions and 859 deletions

View File

@ -540,7 +540,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
OutgoingMessage.prototype.getHeader = function getHeader(name) { OutgoingMessage.prototype.getHeader = function getHeader(name) {
if (typeof name !== 'string') { if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string'); throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
} }
if (!this[outHeadersKey]) return; if (!this[outHeadersKey]) return;
@ -576,7 +576,7 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
OutgoingMessage.prototype.hasHeader = function hasHeader(name) { OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
if (typeof name !== 'string') { if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string'); throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
} }
return !!(this[outHeadersKey] && this[outHeadersKey][name.toLowerCase()]); return !!(this[outHeadersKey] && this[outHeadersKey][name.toLowerCase()]);
@ -585,7 +585,7 @@ OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
OutgoingMessage.prototype.removeHeader = function removeHeader(name) { OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
if (typeof name !== 'string') { if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string'); throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
} }
if (this._header) { if (this._header) {
@ -656,7 +656,8 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
} }
if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) { if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new ERR_INVALID_ARG_TYPE('first argument', ['string', 'Buffer']); throw new ERR_INVALID_ARG_TYPE('first argument',
['string', 'Buffer'], chunk);
} }

View File

@ -294,7 +294,8 @@ function chunkInvalid(state, chunk) {
typeof chunk !== 'string' && typeof chunk !== 'string' &&
chunk !== undefined && chunk !== undefined &&
!state.objectMode) { !state.objectMode) {
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer', 'Uint8Array']); er = new ERR_INVALID_ARG_TYPE(
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
} }
return er; return er;
} }

View File

@ -255,7 +255,7 @@ function validChunk(stream, state, chunk, cb) {
if (chunk === null) { if (chunk === null) {
er = new ERR_STREAM_NULL_VALUES(); er = new ERR_STREAM_NULL_VALUES();
} else if (typeof chunk !== 'string' && !state.objectMode) { } else if (typeof chunk !== 'string' && !state.objectMode) {
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer']); er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
} }
if (er) { if (er) {
stream.emit('error', er); stream.emit('error', er);

View File

@ -58,11 +58,14 @@ function SecureContext(secureProtocol, secureOptions, context) {
} }
function validateKeyCert(value, type) { function validateKeyCert(value, type) {
if (typeof value !== 'string' && !isArrayBufferView(value)) if (typeof value !== 'string' && !isArrayBufferView(value)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
// TODO(BridgeAR): Change this to `options.${type}`
type, type,
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
value
); );
}
} }
exports.SecureContext = SecureContext; exports.SecureContext = SecureContext;

View File

@ -667,7 +667,7 @@ TLSSocket.prototype._start = function() {
TLSSocket.prototype.setServername = function(name) { TLSSocket.prototype.setServername = function(name) {
if (typeof name !== 'string') { if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string'); throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
} }
if (this._tlsOptions.isServer) { if (this._tlsOptions.isServer) {
@ -877,7 +877,7 @@ function Server(options, listener) {
} else if (options == null || typeof options === 'object') { } else if (options == null || typeof options === 'object') {
options = options || {}; options = options || {};
} else { } else {
throw new ERR_INVALID_ARG_TYPE('options', 'Object'); throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
} }

View File

@ -141,7 +141,7 @@ function showEmitBeforeAfterWarning() {
class AsyncResource { class AsyncResource {
constructor(type, opts = {}) { constructor(type, opts = {}) {
if (typeof type !== 'string') if (typeof type !== 'string')
throw new ERR_INVALID_ARG_TYPE('type', 'string'); throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
if (typeof opts === 'number') { if (typeof opts === 'number') {
opts = { triggerAsyncId: opts, requireManualDestroy: false }; opts = { triggerAsyncId: opts, requireManualDestroy: false };

View File

@ -416,16 +416,20 @@ Buffer.isBuffer = function isBuffer(b) {
return b instanceof Buffer; return b instanceof Buffer;
}; };
Buffer.compare = function compare(a, b) { Buffer.compare = function compare(buf1, buf2) {
if (!isUint8Array(a) || !isUint8Array(b)) { if (!isUint8Array(buf1)) {
throw new ERR_INVALID_ARG_TYPE(['buf1', 'buf2'], ['Buffer', 'Uint8Array']); throw new ERR_INVALID_ARG_TYPE('buf1', ['Buffer', 'Uint8Array'], buf1);
} }
if (a === b) { if (!isUint8Array(buf2)) {
throw new ERR_INVALID_ARG_TYPE('buf2', ['Buffer', 'Uint8Array'], buf2);
}
if (buf1 === buf2) {
return 0; return 0;
} }
return _compare(a, b); return _compare(buf1, buf2);
}; };
Buffer.isEncoding = function isEncoding(encoding) { Buffer.isEncoding = function isEncoding(encoding) {
@ -437,7 +441,8 @@ Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
Buffer.concat = function concat(list, length) { Buffer.concat = function concat(list, length) {
var i; var i;
if (!Array.isArray(list)) { if (!Array.isArray(list)) {
throw new ERR_INVALID_ARG_TYPE('list', ['Array', 'Buffer', 'Uint8Array']); throw new ERR_INVALID_ARG_TYPE(
'list', ['Array', 'Buffer', 'Uint8Array'], list);
} }
if (list.length === 0) if (list.length === 0)
@ -645,14 +650,15 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
return stringSlice(this, encoding, start, end); return stringSlice(this, encoding, start, end);
}; };
Buffer.prototype.equals = function equals(b) { Buffer.prototype.equals = function equals(otherBuffer) {
if (!isUint8Array(b)) { if (!isUint8Array(otherBuffer)) {
throw new ERR_INVALID_ARG_TYPE('otherBuffer', ['Buffer', 'Uint8Array'], b); throw new ERR_INVALID_ARG_TYPE(
'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
} }
if (this === b) if (this === otherBuffer)
return true; return true;
return _compare(this, b) === 0; return _compare(this, otherBuffer) === 0;
}; };
// Override how buffers are presented by util.inspect(). // Override how buffers are presented by util.inspect().

View File

@ -75,7 +75,7 @@ function newHandle(type, lookup) {
if (lookup === undefined) if (lookup === undefined)
lookup = dns.lookup; lookup = dns.lookup;
else if (typeof lookup !== 'function') else if (typeof lookup !== 'function')
throw new ERR_INVALID_ARG_TYPE('lookup', 'Function'); throw new ERR_INVALID_ARG_TYPE('lookup', 'Function', lookup);
if (type === 'udp4') { if (type === 'udp4') {
const handle = new UDP(); const handle = new UDP();
@ -299,19 +299,19 @@ Socket.prototype.sendto = function(buffer,
address, address,
callback) { callback) {
if (typeof offset !== 'number') { if (typeof offset !== 'number') {
throw new ERR_INVALID_ARG_TYPE('offset', 'number'); throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
} }
if (typeof length !== 'number') { if (typeof length !== 'number') {
throw new ERR_INVALID_ARG_TYPE('length', 'number'); throw new ERR_INVALID_ARG_TYPE('length', 'number', length);
} }
if (typeof port !== 'number') { if (typeof port !== 'number') {
throw new ERR_INVALID_ARG_TYPE('port', 'number'); throw new ERR_INVALID_ARG_TYPE('port', 'number', port);
} }
if (typeof address !== 'string') { if (typeof address !== 'string') {
throw new ERR_INVALID_ARG_TYPE('address', 'string'); throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
} }
this.send(buffer, offset, length, port, address, callback); this.send(buffer, offset, length, port, address, callback);
@ -323,7 +323,7 @@ function sliceBuffer(buffer, offset, length) {
buffer = Buffer.from(buffer); buffer = Buffer.from(buffer);
} else if (!isUint8Array(buffer)) { } else if (!isUint8Array(buffer)) {
throw new ERR_INVALID_ARG_TYPE('buffer', throw new ERR_INVALID_ARG_TYPE('buffer',
['Buffer', 'Uint8Array', 'string']); ['Buffer', 'Uint8Array', 'string'], buffer);
} }
offset = offset >>> 0; offset = offset >>> 0;
@ -415,13 +415,14 @@ Socket.prototype.send = function(buffer,
list = [ Buffer.from(buffer) ]; list = [ Buffer.from(buffer) ];
} else if (!isUint8Array(buffer)) { } else if (!isUint8Array(buffer)) {
throw new ERR_INVALID_ARG_TYPE('buffer', throw new ERR_INVALID_ARG_TYPE('buffer',
['Buffer', 'Uint8Array', 'string']); ['Buffer', 'Uint8Array', 'string'],
buffer);
} else { } else {
list = [ buffer ]; list = [ buffer ];
} }
} else if (!(list = fixBufferList(buffer))) { } else if (!(list = fixBufferList(buffer))) {
throw new ERR_INVALID_ARG_TYPE('buffer list arguments', throw new ERR_INVALID_ARG_TYPE('buffer list arguments',
['Buffer', 'string']); ['Buffer', 'string'], buffer);
} }
port = port >>> 0; port = port >>> 0;
@ -437,7 +438,7 @@ Socket.prototype.send = function(buffer,
callback = address; callback = address;
address = undefined; address = undefined;
} else if (address && typeof address !== 'string') { } else if (address && typeof address !== 'string') {
throw new ERR_INVALID_ARG_TYPE('address', ['string', 'falsy']); throw new ERR_INVALID_ARG_TYPE('address', ['string', 'falsy'], address);
} }
this._healthCheck(); this._healthCheck();
@ -602,7 +603,8 @@ Socket.prototype.setMulticastInterface = function(interfaceAddress) {
this._healthCheck(); this._healthCheck();
if (typeof interfaceAddress !== 'string') { if (typeof interfaceAddress !== 'string') {
throw new ERR_INVALID_ARG_TYPE('interfaceAddress', 'string'); throw new ERR_INVALID_ARG_TYPE(
'interfaceAddress', 'string', interfaceAddress);
} }
const err = this._handle.setMulticastInterface(interfaceAddress); const err = this._handle.setMulticastInterface(interfaceAddress);

View File

@ -197,7 +197,7 @@ function _addListener(target, type, listener, prepend) {
if (typeof listener !== 'function') { if (typeof listener !== 'function') {
const errors = lazyErrors(); const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function'); throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
} }
events = target._events; events = target._events;
@ -287,7 +287,7 @@ function _onceWrap(target, type, listener) {
EventEmitter.prototype.once = function once(type, listener) { EventEmitter.prototype.once = function once(type, listener) {
if (typeof listener !== 'function') { if (typeof listener !== 'function') {
const errors = lazyErrors(); const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function'); throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
} }
this.on(type, _onceWrap(this, type, listener)); this.on(type, _onceWrap(this, type, listener));
return this; return this;
@ -297,7 +297,7 @@ EventEmitter.prototype.prependOnceListener =
function prependOnceListener(type, listener) { function prependOnceListener(type, listener) {
if (typeof listener !== 'function') { if (typeof listener !== 'function') {
const errors = lazyErrors(); const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function'); throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
} }
this.prependListener(type, _onceWrap(this, type, listener)); this.prependListener(type, _onceWrap(this, type, listener));
return this; return this;
@ -310,7 +310,7 @@ EventEmitter.prototype.removeListener =
if (typeof listener !== 'function') { if (typeof listener !== 'function') {
const errors = lazyErrors(); const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function'); throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
} }
events = this._events; events = this._events;

View File

@ -112,7 +112,7 @@ class FileHandle {
function validateFileHandle(handle) { function validateFileHandle(handle) {
if (!(handle instanceof FileHandle)) if (!(handle instanceof FileHandle))
throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle'); throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle', handle);
} }
async function writeFileHandle(filehandle, data, options) { async function writeFileHandle(filehandle, data, options) {

View File

@ -17,7 +17,8 @@ function verifySpkac(spkac) {
if (!isArrayBufferView(spkac)) { if (!isArrayBufferView(spkac)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'spkac', 'spkac',
['Buffer', 'TypedArray', 'DataView'] ['Buffer', 'TypedArray', 'DataView'],
spkac
); );
} }
return certVerifySpkac(spkac); return certVerifySpkac(spkac);
@ -28,7 +29,8 @@ function exportPublicKey(spkac, encoding) {
if (!isArrayBufferView(spkac)) { if (!isArrayBufferView(spkac)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'spkac', 'spkac',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
spkac
); );
} }
return certExportPublicKey(spkac); return certExportPublicKey(spkac);
@ -39,7 +41,8 @@ function exportChallenge(spkac, encoding) {
if (!isArrayBufferView(spkac)) { if (!isArrayBufferView(spkac)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'spkac', 'spkac',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
spkac
); );
} }
return certExportChallenge(spkac); return certExportChallenge(spkac);

View File

@ -67,13 +67,14 @@ function Cipher(cipher, password, options) {
return new Cipher(cipher, password, options); return new Cipher(cipher, password, options);
if (typeof cipher !== 'string') if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string'); throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
password = toBuf(password); password = toBuf(password);
if (!isArrayBufferView(password)) { if (!isArrayBufferView(password)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'password', 'password',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
password
); );
} }
@ -110,7 +111,8 @@ Cipher.prototype.update = function update(data, inputEncoding, outputEncoding) {
if (typeof data !== 'string' && !isArrayBufferView(data)) { if (typeof data !== 'string' && !isArrayBufferView(data)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'data', 'data',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
data
); );
} }
@ -155,7 +157,8 @@ Cipher.prototype.getAuthTag = function getAuthTag() {
Cipher.prototype.setAuthTag = function setAuthTag(tagbuf) { Cipher.prototype.setAuthTag = function setAuthTag(tagbuf) {
if (!isArrayBufferView(tagbuf)) { if (!isArrayBufferView(tagbuf)) {
throw new ERR_INVALID_ARG_TYPE('buffer', throw new ERR_INVALID_ARG_TYPE('buffer',
['Buffer', 'TypedArray', 'DataView']); ['Buffer', 'TypedArray', 'DataView'],
tagbuf);
} }
// Do not do a normal falsy check because the method returns // Do not do a normal falsy check because the method returns
// undefined if it succeeds. Returns false specifically if it // undefined if it succeeds. Returns false specifically if it
@ -168,7 +171,8 @@ Cipher.prototype.setAuthTag = function setAuthTag(tagbuf) {
Cipher.prototype.setAAD = function setAAD(aadbuf) { Cipher.prototype.setAAD = function setAAD(aadbuf) {
if (!isArrayBufferView(aadbuf)) { if (!isArrayBufferView(aadbuf)) {
throw new ERR_INVALID_ARG_TYPE('buffer', throw new ERR_INVALID_ARG_TYPE('buffer',
['Buffer', 'TypedArray', 'DataView']); ['Buffer', 'TypedArray', 'DataView'],
aadbuf);
} }
if (this._handle.setAAD(aadbuf) === false) if (this._handle.setAAD(aadbuf) === false)
throw new ERR_CRYPTO_INVALID_STATE('setAAD'); throw new ERR_CRYPTO_INVALID_STATE('setAAD');
@ -180,13 +184,14 @@ function Cipheriv(cipher, key, iv, options) {
return new Cipheriv(cipher, key, iv, options); return new Cipheriv(cipher, key, iv, options);
if (typeof cipher !== 'string') if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string'); throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
key = toBuf(key); key = toBuf(key);
if (!isArrayBufferView(key)) { if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'key', 'key',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
key
); );
} }
@ -194,7 +199,8 @@ function Cipheriv(cipher, key, iv, options) {
if (iv !== null && !isArrayBufferView(iv)) { if (iv !== null && !isArrayBufferView(iv)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'iv', 'iv',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
iv
); );
} }
@ -226,13 +232,14 @@ function Decipher(cipher, password, options) {
return new Decipher(cipher, password, options); return new Decipher(cipher, password, options);
if (typeof cipher !== 'string') if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string'); throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
password = toBuf(password); password = toBuf(password);
if (!isArrayBufferView(password)) { if (!isArrayBufferView(password)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'password', 'password',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
password
); );
} }
@ -261,13 +268,14 @@ function Decipheriv(cipher, key, iv, options) {
return new Decipheriv(cipher, key, iv, options); return new Decipheriv(cipher, key, iv, options);
if (typeof cipher !== 'string') if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string'); throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
key = toBuf(key); key = toBuf(key);
if (!isArrayBufferView(key)) { if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'key', 'key',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
key
); );
} }
@ -275,7 +283,8 @@ function Decipheriv(cipher, key, iv, options) {
if (iv !== null && !isArrayBufferView(iv)) { if (iv !== null && !isArrayBufferView(iv)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'iv', 'iv',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
iv
); );
} }

View File

@ -34,7 +34,8 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
!isArrayBufferView(sizeOrKey)) { !isArrayBufferView(sizeOrKey)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'sizeOrKey', 'sizeOrKey',
['number', 'string', 'Buffer', 'TypedArray', 'DataView'] ['number', 'string', 'Buffer', 'TypedArray', 'DataView'],
sizeOrKey
); );
} }
@ -169,7 +170,7 @@ function ECDH(curve) {
return new ECDH(curve); return new ECDH(curve);
if (typeof curve !== 'string') if (typeof curve !== 'string')
throw new ERR_INVALID_ARG_TYPE('curve', 'string'); throw new ERR_INVALID_ARG_TYPE('curve', 'string', curve);
this._handle = new _ECDH(curve); this._handle = new _ECDH(curve);
} }
@ -196,12 +197,13 @@ ECDH.convertKey = function convertKey(key, curve, inEnc, outEnc, format) {
if (typeof key !== 'string' && !isArrayBufferView(key)) { if (typeof key !== 'string' && !isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'key', 'key',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
key
); );
} }
if (typeof curve !== 'string') { if (typeof curve !== 'string') {
throw new ERR_INVALID_ARG_TYPE('curve', 'string'); throw new ERR_INVALID_ARG_TYPE('curve', 'string', curve);
} }
const encoding = getDefaultEncoding(); const encoding = getDefaultEncoding();

View File

@ -29,7 +29,7 @@ function Hash(algorithm, options) {
if (!(this instanceof Hash)) if (!(this instanceof Hash))
return new Hash(algorithm, options); return new Hash(algorithm, options);
if (typeof algorithm !== 'string') if (typeof algorithm !== 'string')
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string'); throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
this._handle = new _Hash(algorithm); this._handle = new _Hash(algorithm);
this[kState] = { this[kState] = {
[kFinalized]: false [kFinalized]: false
@ -56,7 +56,7 @@ Hash.prototype.update = function update(data, encoding) {
if (typeof data !== 'string' && !isArrayBufferView(data)) { if (typeof data !== 'string' && !isArrayBufferView(data)) {
throw new ERR_INVALID_ARG_TYPE('data', throw new ERR_INVALID_ARG_TYPE('data',
['string', 'TypedArray', 'DataView']); ['string', 'TypedArray', 'DataView'], data);
} }
if (!this._handle.update(data, encoding || getDefaultEncoding())) if (!this._handle.update(data, encoding || getDefaultEncoding()))
@ -84,9 +84,10 @@ function Hmac(hmac, key, options) {
if (!(this instanceof Hmac)) if (!(this instanceof Hmac))
return new Hmac(hmac, key, options); return new Hmac(hmac, key, options);
if (typeof hmac !== 'string') if (typeof hmac !== 'string')
throw new ERR_INVALID_ARG_TYPE('hmac', 'string'); throw new ERR_INVALID_ARG_TYPE('hmac', 'string', hmac);
if (typeof key !== 'string' && !isArrayBufferView(key)) { if (typeof key !== 'string' && !isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE('key', ['string', 'TypedArray', 'DataView']); throw new ERR_INVALID_ARG_TYPE('key',
['string', 'TypedArray', 'DataView'], key);
} }
this._handle = new _Hmac(); this._handle = new _Hmac();
this._handle.init(hmac, toBuf(key)); this._handle.init(hmac, toBuf(key));

View File

@ -37,22 +37,24 @@ function pbkdf2Sync(password, salt, iterations, keylen, digest) {
function _pbkdf2(password, salt, iterations, keylen, digest, callback) { function _pbkdf2(password, salt, iterations, keylen, digest, callback) {
if (digest !== null && typeof digest !== 'string') if (digest !== null && typeof digest !== 'string')
throw new ERR_INVALID_ARG_TYPE('digest', ['string', 'null']); throw new ERR_INVALID_ARG_TYPE('digest', ['string', 'null'], digest);
password = toBuf(password); password = toBuf(password);
salt = toBuf(salt); salt = toBuf(salt);
if (!isArrayBufferView(password)) { if (!isArrayBufferView(password)) {
throw new ERR_INVALID_ARG_TYPE('password', throw new ERR_INVALID_ARG_TYPE('password',
['string', 'Buffer', 'TypedArray']); ['string', 'Buffer', 'TypedArray'],
password);
} }
if (!isArrayBufferView(salt)) { if (!isArrayBufferView(salt)) {
throw new ERR_INVALID_ARG_TYPE('salt', ['string', 'Buffer', 'TypedArray']); throw new ERR_INVALID_ARG_TYPE('salt',
['string', 'Buffer', 'TypedArray'], salt);
} }
if (typeof iterations !== 'number') if (typeof iterations !== 'number')
throw new ERR_INVALID_ARG_TYPE('iterations', 'number'); throw new ERR_INVALID_ARG_TYPE('iterations', 'number', iterations);
if (iterations < 0) if (iterations < 0)
throw new ERR_OUT_OF_RANGE('iterations', throw new ERR_OUT_OF_RANGE('iterations',
@ -60,7 +62,7 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) {
iterations); iterations);
if (typeof keylen !== 'number') if (typeof keylen !== 'number')
throw new ERR_INVALID_ARG_TYPE('keylen', 'number'); throw new ERR_INVALID_ARG_TYPE('keylen', 'number', keylen);
if (keylen < 0 || if (keylen < 0 ||
!Number.isFinite(keylen) || !Number.isFinite(keylen) ||

View File

@ -51,7 +51,7 @@ function randomBytes(size, cb) {
function randomFillSync(buf, offset = 0, size) { function randomFillSync(buf, offset = 0, size) {
if (!isArrayBufferView(buf)) { if (!isArrayBufferView(buf)) {
throw new ERR_INVALID_ARG_TYPE('buf', 'ArrayBufferView'); throw new ERR_INVALID_ARG_TYPE('buf', 'ArrayBufferView', buf);
} }
const elementSize = buf.BYTES_PER_ELEMENT || 1; const elementSize = buf.BYTES_PER_ELEMENT || 1;
@ -72,7 +72,7 @@ function randomFillSync(buf, offset = 0, size) {
function randomFill(buf, offset, size, cb) { function randomFill(buf, offset, size, cb) {
if (!isArrayBufferView(buf)) { if (!isArrayBufferView(buf)) {
throw new ERR_INVALID_ARG_TYPE('buf', 'ArrayBufferView'); throw new ERR_INVALID_ARG_TYPE('buf', 'ArrayBufferView', buf);
} }
const elementSize = buf.BYTES_PER_ELEMENT || 1; const elementSize = buf.BYTES_PER_ELEMENT || 1;

View File

@ -25,7 +25,7 @@ function Sign(algorithm, options) {
if (!(this instanceof Sign)) if (!(this instanceof Sign))
return new Sign(algorithm, options); return new Sign(algorithm, options);
if (typeof algorithm !== 'string') if (typeof algorithm !== 'string')
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string'); throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
this._handle = new _Sign(); this._handle = new _Sign();
this._handle.init(algorithm); this._handle.init(algorithm);
@ -45,7 +45,8 @@ Sign.prototype.update = function update(data, encoding) {
if (!isArrayBufferView(data)) { if (!isArrayBufferView(data)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'data', 'data',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
data
); );
} }
this._handle.update(data); this._handle.update(data);
@ -82,7 +83,8 @@ Sign.prototype.sign = function sign(options, encoding) {
if (!isArrayBufferView(key)) { if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'key', 'key',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
key
); );
} }
@ -100,7 +102,7 @@ function Verify(algorithm, options) {
if (!(this instanceof Verify)) if (!(this instanceof Verify))
return new Verify(algorithm, options); return new Verify(algorithm, options);
if (typeof algorithm !== 'string') if (typeof algorithm !== 'string')
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string'); throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
this._handle = new _Verify(); this._handle = new _Verify();
this._handle.init(algorithm); this._handle.init(algorithm);
@ -139,7 +141,8 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
if (!isArrayBufferView(key)) { if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'key', 'key',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
key
); );
} }
@ -147,7 +150,8 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
if (!isArrayBufferView(signature)) { if (!isArrayBufferView(signature)) {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'signature', 'signature',
['string', 'Buffer', 'TypedArray', 'DataView'] ['string', 'Buffer', 'TypedArray', 'DataView'],
signature
); );
} }

View File

@ -54,10 +54,10 @@ const getCurves = cachedResult(() => filterDuplicateStrings(_getCurves()));
function setEngine(id, flags) { function setEngine(id, flags) {
if (typeof id !== 'string') if (typeof id !== 'string')
throw new ERR_INVALID_ARG_TYPE('id', 'string'); throw new ERR_INVALID_ARG_TYPE('id', 'string', id);
if (flags && typeof flags !== 'number') if (flags && typeof flags !== 'number')
throw new ERR_INVALID_ARG_TYPE('flags', 'number'); throw new ERR_INVALID_ARG_TYPE('flags', 'number', flags);
flags = flags >>> 0; flags = flags >>> 0;
// Use provided engine for everything by default // Use provided engine for everything by default
@ -68,17 +68,19 @@ function setEngine(id, flags) {
throw new ERR_CRYPTO_ENGINE_UNKNOWN(id); throw new ERR_CRYPTO_ENGINE_UNKNOWN(id);
} }
function timingSafeEqual(a, b) { function timingSafeEqual(buf1, buf2) {
if (!isArrayBufferView(a)) { if (!isArrayBufferView(buf1)) {
throw new ERR_INVALID_ARG_TYPE('a', ['Buffer', 'TypedArray', 'DataView']); throw new ERR_INVALID_ARG_TYPE('buf1',
['Buffer', 'TypedArray', 'DataView'], buf1);
} }
if (!isArrayBufferView(b)) { if (!isArrayBufferView(buf2)) {
throw new ERR_INVALID_ARG_TYPE('b', ['Buffer', 'TypedArray', 'DataView']); throw new ERR_INVALID_ARG_TYPE('buf2',
['Buffer', 'TypedArray', 'DataView'], buf2);
} }
if (a.length !== b.length) { if (buf1.length !== buf2.length) {
throw new ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH(); throw new ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH();
} }
return _timingSafeEqual(a, b); return _timingSafeEqual(buf1, buf2);
} }
module.exports = { module.exports = {

View File

@ -51,7 +51,7 @@ function validateDecoder(obj) {
function validateArgument(prop, expected, propName, expectedName) { function validateArgument(prop, expected, propName, expectedName) {
if (typeof prop !== expected) if (typeof prop !== expected)
throw new ERR_INVALID_ARG_TYPE(propName, expectedName); throw new ERR_INVALID_ARG_TYPE(propName, expectedName, prop);
} }
const CONVERTER_FLAGS_FLUSH = 0x1; const CONVERTER_FLAGS_FLUSH = 0x1;
@ -391,7 +391,8 @@ function makeTextDecoderICU() {
input = lazyBuffer().from(input); input = lazyBuffer().from(input);
} else if (!isArrayBufferView(input)) { } else if (!isArrayBufferView(input)) {
throw new ERR_INVALID_ARG_TYPE('input', throw new ERR_INVALID_ARG_TYPE('input',
['ArrayBuffer', 'ArrayBufferView']); ['ArrayBuffer', 'ArrayBufferView'],
input);
} }
validateArgument(options, 'object', 'options', 'Object'); validateArgument(options, 'object', 'options', 'Object');
@ -460,7 +461,8 @@ function makeTextDecoderJS() {
input.byteLength); input.byteLength);
} else { } else {
throw new ERR_INVALID_ARG_TYPE('input', throw new ERR_INVALID_ARG_TYPE('input',
['ArrayBuffer', 'ArrayBufferView']); ['ArrayBuffer', 'ArrayBufferView'],
input);
} }
validateArgument(options, 'object', 'options', 'Object'); validateArgument(options, 'object', 'options', 'Object');

View File

@ -319,7 +319,7 @@ function createErrDiff(actual, expected, operator) {
class AssertionError extends Error { class AssertionError extends Error {
constructor(options) { constructor(options) {
if (typeof options !== 'object' || options === null) { if (typeof options !== 'object' || options === null) {
throw new codes.ERR_INVALID_ARG_TYPE('options', 'Object'); throw new codes.ERR_INVALID_ARG_TYPE('options', 'Object', options);
} }
var { var {
actual, actual,
@ -959,10 +959,10 @@ function invalidArgType(name, expected, actual) {
msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`; msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
} }
// if actual value received, output it // If actual value received, output it
if (arguments.length >= 3) { // TODO(BridgeAR): Improve the output by showing `null` and similar.
msg += `. Received type ${actual !== null ? typeof actual : 'null'}`; if (arguments.length === 3)
} msg += `. Received type ${typeof actual}`;
return msg; return msg;
} }

View File

@ -323,7 +323,8 @@ function toUnixTimestamp(time, name = 'time') {
function validateBuffer(buffer) { function validateBuffer(buffer) {
if (!isUint8Array(buffer)) { if (!isUint8Array(buffer)) {
const err = new ERR_INVALID_ARG_TYPE('buffer', ['Buffer', 'Uint8Array']); const err = new ERR_INVALID_ARG_TYPE('buffer',
['Buffer', 'Uint8Array'], buffer);
Error.captureStackTrace(err, validateBuffer); Error.captureStackTrace(err, validateBuffer);
throw err; throw err;
} }
@ -379,7 +380,7 @@ function validatePath(path, propName) {
} }
if (typeof path !== 'string' && !isUint8Array(path)) { if (typeof path !== 'string' && !isUint8Array(path)) {
err = new ERR_INVALID_ARG_TYPE(propName, ['string', 'Buffer', 'URL']); err = new ERR_INVALID_ARG_TYPE(propName, ['string', 'Buffer', 'URL'], path);
} else { } else {
err = nullCheck(path, propName, false); err = nullCheck(path, propName, false);
} }

View File

@ -438,7 +438,7 @@ class Http2ServerResponse extends Stream {
setTrailer(name, value) { setTrailer(name, value) {
if (typeof name !== 'string') if (typeof name !== 'string')
throw new ERR_INVALID_ARG_TYPE('name', 'string'); throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
name = name.trim().toLowerCase(); name = name.trim().toLowerCase();
assertValidHeader(name, value); assertValidHeader(name, value);
@ -456,7 +456,7 @@ class Http2ServerResponse extends Stream {
getHeader(name) { getHeader(name) {
if (typeof name !== 'string') if (typeof name !== 'string')
throw new ERR_INVALID_ARG_TYPE('name', 'string'); throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
name = name.trim().toLowerCase(); name = name.trim().toLowerCase();
return this[kHeaders][name]; return this[kHeaders][name];
@ -472,7 +472,7 @@ class Http2ServerResponse extends Stream {
hasHeader(name) { hasHeader(name) {
if (typeof name !== 'string') if (typeof name !== 'string')
throw new ERR_INVALID_ARG_TYPE('name', 'string'); throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
name = name.trim().toLowerCase(); name = name.trim().toLowerCase();
return Object.prototype.hasOwnProperty.call(this[kHeaders], name); return Object.prototype.hasOwnProperty.call(this[kHeaders], name);
@ -480,7 +480,7 @@ class Http2ServerResponse extends Stream {
removeHeader(name) { removeHeader(name) {
if (typeof name !== 'string') if (typeof name !== 'string')
throw new ERR_INVALID_ARG_TYPE('name', 'string'); throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
if (this[kStream].headersSent) if (this[kStream].headersSent)
throw new ERR_HTTP2_HEADERS_SENT(); throw new ERR_HTTP2_HEADERS_SENT();
@ -491,7 +491,7 @@ class Http2ServerResponse extends Stream {
setHeader(name, value) { setHeader(name, value) {
if (typeof name !== 'string') if (typeof name !== 'string')
throw new ERR_INVALID_ARG_TYPE('name', 'string'); throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
if (this[kStream].headersSent) if (this[kStream].headersSent)
throw new ERR_HTTP2_HEADERS_SENT(); throw new ERR_HTTP2_HEADERS_SENT();

View File

@ -984,7 +984,7 @@ class Http2Session extends EventEmitter {
throw new ERR_HTTP2_INVALID_SESSION(); throw new ERR_HTTP2_INVALID_SESSION();
if (typeof id !== 'number') if (typeof id !== 'number')
throw new ERR_INVALID_ARG_TYPE('id', 'number'); throw new ERR_INVALID_ARG_TYPE('id', 'number', id);
if (id <= 0 || id > kMaxStreams) if (id <= 0 || id > kMaxStreams)
throw new ERR_OUT_OF_RANGE('id', `> 0 and <= ${kMaxStreams}`, id); throw new ERR_OUT_OF_RANGE('id', `> 0 and <= ${kMaxStreams}`, id);
this[kHandle].setNextStreamID(id); this[kHandle].setNextStreamID(id);
@ -1003,7 +1003,8 @@ class Http2Session extends EventEmitter {
} }
if (payload && !isArrayBufferView(payload)) { if (payload && !isArrayBufferView(payload)) {
throw new ERR_INVALID_ARG_TYPE('payload', throw new ERR_INVALID_ARG_TYPE('payload',
['Buffer', 'TypedArray', 'DataView']); ['Buffer', 'TypedArray', 'DataView'],
payload);
} }
if (payload && payload.length !== 8) { if (payload && payload.length !== 8) {
throw new ERR_HTTP2_PING_LENGTH(); throw new ERR_HTTP2_PING_LENGTH();
@ -1122,13 +1123,14 @@ class Http2Session extends EventEmitter {
if (opaqueData !== undefined && !isArrayBufferView(opaqueData)) { if (opaqueData !== undefined && !isArrayBufferView(opaqueData)) {
throw new ERR_INVALID_ARG_TYPE('opaqueData', throw new ERR_INVALID_ARG_TYPE('opaqueData',
['Buffer', 'TypedArray', 'DataView']); ['Buffer', 'TypedArray', 'DataView'],
opaqueData);
} }
if (typeof code !== 'number') { if (typeof code !== 'number') {
throw new ERR_INVALID_ARG_TYPE('code', 'number'); throw new ERR_INVALID_ARG_TYPE('code', 'number', code);
} }
if (typeof lastStreamID !== 'number') { if (typeof lastStreamID !== 'number') {
throw new ERR_INVALID_ARG_TYPE('lastStreamID', 'number'); throw new ERR_INVALID_ARG_TYPE('lastStreamID', 'number', lastStreamID);
} }
const goawayFn = submitGoaway.bind(this, code, lastStreamID, opaqueData); const goawayFn = submitGoaway.bind(this, code, lastStreamID, opaqueData);
@ -1321,14 +1323,15 @@ class ServerHttp2Session extends Http2Session {
// be invalid. // be invalid.
if (typeof origin !== 'string') { if (typeof origin !== 'string') {
throw new ERR_INVALID_ARG_TYPE('originOrStream', throw new ERR_INVALID_ARG_TYPE('originOrStream',
['string', 'number', 'URL', 'object']); ['string', 'number', 'URL', 'object'],
originOrStream);
} else if (origin === 'null' || origin.length === 0) { } else if (origin === 'null' || origin.length === 0) {
throw new ERR_HTTP2_ALTSVC_INVALID_ORIGIN(); throw new ERR_HTTP2_ALTSVC_INVALID_ORIGIN();
} }
} }
if (typeof alt !== 'string') if (typeof alt !== 'string')
throw new ERR_INVALID_ARG_TYPE('alt', 'string'); throw new ERR_INVALID_ARG_TYPE('alt', 'string', alt);
if (!kQuotedString.test(alt)) if (!kQuotedString.test(alt))
throw new ERR_INVALID_CHAR('alt'); throw new ERR_INVALID_CHAR('alt');
@ -1794,7 +1797,7 @@ class Http2Stream extends Duplex {
// but no DATA and HEADERS frames may be sent. // but no DATA and HEADERS frames may be sent.
close(code = NGHTTP2_NO_ERROR, callback) { close(code = NGHTTP2_NO_ERROR, callback) {
if (typeof code !== 'number') if (typeof code !== 'number')
throw new ERR_INVALID_ARG_TYPE('code', 'number'); throw new ERR_INVALID_ARG_TYPE('code', 'number', code);
if (code < 0 || code > kMaxInt) if (code < 0 || code > kMaxInt)
throw new ERR_OUT_OF_RANGE('code'); throw new ERR_OUT_OF_RANGE('code');
if (callback !== undefined && typeof callback !== 'function') if (callback !== undefined && typeof callback !== 'function')
@ -2313,7 +2316,7 @@ class ServerHttp2Stream extends Http2Stream {
} }
if (typeof fd !== 'number') if (typeof fd !== 'number')
throw new ERR_INVALID_ARG_TYPE('fd', 'number'); throw new ERR_INVALID_ARG_TYPE('fd', 'number', fd);
debug(`Http2Stream ${this[kID]} [Http2Session ` + debug(`Http2Stream ${this[kID]} [Http2Session ` +
`${sessionName(session[kType])}]: initiating response from fd`); `${sessionName(session[kType])}]: initiating response from fd`);
@ -2767,7 +2770,8 @@ function getPackedSettings(settings) {
function getUnpackedSettings(buf, options = {}) { function getUnpackedSettings(buf, options = {}) {
if (!isArrayBufferView(buf)) { if (!isArrayBufferView(buf)) {
throw new ERR_INVALID_ARG_TYPE('buf', ['Buffer', 'TypedArray', 'DataView']); throw new ERR_INVALID_ARG_TYPE('buf',
['Buffer', 'TypedArray', 'DataView'], buf);
} }
if (buf.length % 6 !== 0) if (buf.length % 6 !== 0)
throw new ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH(); throw new ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH();

View File

@ -482,7 +482,7 @@ function assertIsObject(value, name, types = 'Object') {
(value === null || (value === null ||
typeof value !== 'object' || typeof value !== 'object' ||
Array.isArray(value))) { Array.isArray(value))) {
const err = new ERR_INVALID_ARG_TYPE(name, types); const err = new ERR_INVALID_ARG_TYPE(name, types, value);
Error.captureStackTrace(err, assertIsObject); Error.captureStackTrace(err, assertIsObject);
throw err; throw err;
} }

View File

@ -49,16 +49,16 @@ class Loader {
async resolve(specifier, parentURL) { async resolve(specifier, parentURL) {
const isMain = parentURL === undefined; const isMain = parentURL === undefined;
if (!isMain && typeof parentURL !== 'string') if (!isMain && typeof parentURL !== 'string')
throw new ERR_INVALID_ARG_TYPE('parentURL', 'string'); throw new ERR_INVALID_ARG_TYPE('parentURL', 'string', parentURL);
const { url, format } = const { url, format } =
await this._resolve(specifier, parentURL, defaultResolve); await this._resolve(specifier, parentURL, defaultResolve);
if (typeof url !== 'string') if (typeof url !== 'string')
throw new ERR_INVALID_ARG_TYPE('url', 'string'); throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
if (typeof format !== 'string') if (typeof format !== 'string')
throw new ERR_INVALID_ARG_TYPE('format', 'string'); throw new ERR_INVALID_ARG_TYPE('format', 'string', format);
if (format === 'builtin') if (format === 'builtin')
return { url: `node:${url}`, format }; return { url: `node:${url}`, format };

View File

@ -9,23 +9,23 @@ const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
class ModuleMap extends SafeMap { class ModuleMap extends SafeMap {
get(url) { get(url) {
if (typeof url !== 'string') { if (typeof url !== 'string') {
throw new ERR_INVALID_ARG_TYPE('url', 'string'); throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
} }
return super.get(url); return super.get(url);
} }
set(url, job) { set(url, job) {
if (typeof url !== 'string') { if (typeof url !== 'string') {
throw new ERR_INVALID_ARG_TYPE('url', 'string'); throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
} }
if (job instanceof ModuleJob !== true) { if (job instanceof ModuleJob !== true) {
throw new ERR_INVALID_ARG_TYPE('job', 'ModuleJob'); throw new ERR_INVALID_ARG_TYPE('job', 'ModuleJob', job);
} }
debug(`Storing ${url} in ModuleMap`); debug(`Storing ${url} in ModuleMap`);
return super.set(url, job); return super.set(url, job);
} }
has(url) { has(url) {
if (typeof url !== 'string') { if (typeof url !== 'string') {
throw new ERR_INVALID_ARG_TYPE('url', 'string'); throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
} }
return super.has(url); return super.has(url);
} }

View File

@ -157,7 +157,7 @@ function setupKillAndExit() {
// eslint-disable-next-line eqeqeq // eslint-disable-next-line eqeqeq
if (pid != (pid | 0)) { if (pid != (pid | 0)) {
throw new ERR_INVALID_ARG_TYPE('pid', 'number'); throw new ERR_INVALID_ARG_TYPE('pid', 'number', pid);
} }
// preserve null signal // preserve null signal
@ -255,7 +255,7 @@ function setupUncaughtExceptionCapture(exceptionHandlerState) {
return; return;
} }
if (typeof fn !== 'function') { if (typeof fn !== 'function') {
throw new ERR_INVALID_ARG_TYPE('fn', ['Function', 'null']); throw new ERR_INVALID_ARG_TYPE('fn', ['Function', 'null'], fn);
} }
if (exceptionHandlerState.captureFn !== null) { if (exceptionHandlerState.captureFn !== null) {
throw new ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET(); throw new ERR_UNCAUGHT_EXCEPTION_CAPTURE_ALREADY_SET();

View File

@ -122,9 +122,9 @@ function setupProcessWarnings() {
code = undefined; code = undefined;
} }
if (code !== undefined && typeof code !== 'string') if (code !== undefined && typeof code !== 'string')
throw new ERR_INVALID_ARG_TYPE('code', 'string'); throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
if (type !== undefined && typeof type !== 'string') if (type !== undefined && typeof type !== 'string')
throw new ERR_INVALID_ARG_TYPE('type', 'string'); throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
if (warning === undefined || typeof warning === 'string') { if (warning === undefined || typeof warning === 'string') {
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
warning = new Error(warning); warning = new Error(warning);
@ -134,7 +134,7 @@ function setupProcessWarnings() {
Error.captureStackTrace(warning, ctor || process.emitWarning); Error.captureStackTrace(warning, ctor || process.emitWarning);
} }
if (!(warning instanceof Error)) { if (!(warning instanceof Error)) {
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string']); throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
} }
if (warning.name === 'DeprecationWarning') { if (warning.name === 'DeprecationWarning') {
if (process.noDeprecation) if (process.noDeprecation)

View File

@ -379,7 +379,7 @@ Object.defineProperties(URL.prototype, {
// eslint-disable-next-line func-name-matching // eslint-disable-next-line func-name-matching
value: function format(options) { value: function format(options) {
if (options && typeof options !== 'object') if (options && typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'Object'); throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
options = util._extend({ options = util._extend({
fragment: true, fragment: true,
unicode: false, unicode: false,

View File

@ -49,7 +49,7 @@ function deprecate(fn, msg, code) {
} }
if (code !== undefined && typeof code !== 'string') if (code !== undefined && typeof code !== 'string')
throw new ERR_INVALID_ARG_TYPE('code', 'string'); throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
let warned = false; let warned = false;
function deprecated(...args) { function deprecated(...args) {
@ -294,7 +294,7 @@ const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');
function promisify(original) { function promisify(original) {
if (typeof original !== 'function') if (typeof original !== 'function')
throw new ERR_INVALID_ARG_TYPE('original', 'Function'); throw new ERR_INVALID_ARG_TYPE('original', 'Function', original);
if (original[kCustomPromisifiedSymbol]) { if (original[kCustomPromisifiedSymbol]) {
const fn = original[kCustomPromisifiedSymbol]; const fn = original[kCustomPromisifiedSymbol];

View File

@ -58,7 +58,8 @@ class Module {
if (isContext(options.context)) { if (isContext(options.context)) {
context = options.context; context = options.context;
} else { } else {
throw new ERR_INVALID_ARG_TYPE('options.context', 'vm.Context'); throw new ERR_INVALID_ARG_TYPE('options.context',
'vm.Context', options.context);
} }
} }

View File

@ -36,7 +36,7 @@ const {
function assertPath(path) { function assertPath(path) {
if (typeof path !== 'string') { if (typeof path !== 'string') {
throw new ERR_INVALID_ARG_TYPE('path', 'string'); throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
} }
} }
@ -747,7 +747,7 @@ const win32 = {
basename: function basename(path, ext) { basename: function basename(path, ext) {
if (ext !== undefined && typeof ext !== 'string') if (ext !== undefined && typeof ext !== 'string')
throw new ERR_INVALID_ARG_TYPE('ext', 'string'); throw new ERR_INVALID_ARG_TYPE('ext', 'string', ext);
assertPath(path); assertPath(path);
var start = 0; var start = 0;
var end = -1; var end = -1;
@ -1295,7 +1295,7 @@ const posix = {
basename: function basename(path, ext) { basename: function basename(path, ext) {
if (ext !== undefined && typeof ext !== 'string') if (ext !== undefined && typeof ext !== 'string')
throw new ERR_INVALID_ARG_TYPE('ext', 'string'); throw new ERR_INVALID_ARG_TYPE('ext', 'string', ext);
assertPath(path); assertPath(path);
var start = 0; var start = 0;

View File

@ -383,7 +383,7 @@ class PerformanceObserver extends AsyncResource {
observe(options) { observe(options) {
const errors = lazyErrors(); const errors = lazyErrors();
if (typeof options !== 'object' || options == null) { if (typeof options !== 'object' || options == null) {
throw new errors.ERR_INVALID_ARG_TYPE('options', 'Object'); throw new errors.ERR_INVALID_ARG_TYPE('options', 'Object', options);
} }
if (!Array.isArray(options.entryTypes)) { if (!Array.isArray(options.entryTypes)) {
throw new errors.ERR_INVALID_OPT_VALUE('entryTypes', options); throw new errors.ERR_INVALID_OPT_VALUE('entryTypes', options);
@ -420,7 +420,7 @@ class Performance extends PerformanceObserverEntryList {
set maxEntries(val) { set maxEntries(val) {
if (typeof val !== 'number' || val >>> 0 !== val) { if (typeof val !== 'number' || val >>> 0 !== val) {
const errors = lazyErrors(); const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('val', 'number'); throw new errors.ERR_INVALID_ARG_TYPE('val', 'number', val);
} }
this[kMaxCount] = Math.max(1, val >>> 0); this[kMaxCount] = Math.max(1, val >>> 0);
} }
@ -533,7 +533,7 @@ class Performance extends PerformanceObserverEntryList {
timerify(fn) { timerify(fn) {
if (typeof fn !== 'function') { if (typeof fn !== 'function') {
const errors = lazyErrors(); const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('fn', 'Function'); throw new errors.ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
} }
if (fn[kTimerified]) if (fn[kTimerified])
return fn[kTimerified]; return fn[kTimerified];

View File

@ -73,7 +73,8 @@ StringDecoder.prototype.write = function write(buf) {
return buf; return buf;
if (!ArrayBuffer.isView(buf)) if (!ArrayBuffer.isView(buf))
throw new ERR_INVALID_ARG_TYPE('buf', throw new ERR_INVALID_ARG_TYPE('buf',
['Buffer', 'Uint8Array', 'ArrayBufferView']); ['Buffer', 'Uint8Array', 'ArrayBufferView'],
buf);
return decode(this[kNativeDecoder], buf); return decode(this[kNativeDecoder], buf);
}; };

View File

@ -323,7 +323,7 @@ Object.defineProperty(inspect, 'defaultOptions', {
}, },
set(options) { set(options) {
if (options === null || typeof options !== 'object') { if (options === null || typeof options !== 'object') {
throw new ERR_INVALID_ARG_TYPE('options', 'Object'); throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
} }
return _extend(inspectDefaultOptions, options); return _extend(inspectDefaultOptions, options);
} }
@ -1024,13 +1024,14 @@ function log() {
function inherits(ctor, superCtor) { function inherits(ctor, superCtor) {
if (ctor === undefined || ctor === null) if (ctor === undefined || ctor === null)
throw new ERR_INVALID_ARG_TYPE('ctor', 'Function'); throw new ERR_INVALID_ARG_TYPE('ctor', 'Function', ctor);
if (superCtor === undefined || superCtor === null) if (superCtor === undefined || superCtor === null)
throw new ERR_INVALID_ARG_TYPE('superCtor', 'Function'); throw new ERR_INVALID_ARG_TYPE('superCtor', 'Function', superCtor);
if (superCtor.prototype === undefined) { if (superCtor.prototype === undefined) {
throw new ERR_INVALID_ARG_TYPE('superCtor.prototype', 'Function'); throw new ERR_INVALID_ARG_TYPE('superCtor.prototype',
'Function', superCtor.prototype);
} }
ctor.super_ = superCtor; ctor.super_ = superCtor;
Object.setPrototypeOf(ctor.prototype, superCtor.prototype); Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
@ -1088,7 +1089,7 @@ function callbackifyOnRejected(reason, cb) {
function callbackify(original) { function callbackify(original) {
if (typeof original !== 'function') { if (typeof original !== 'function') {
throw new ERR_INVALID_ARG_TYPE('original', 'Function'); throw new ERR_INVALID_ARG_TYPE('original', 'Function', original);
} }
// We DO NOT return the promise as it gives the user a false sense that // We DO NOT return the promise as it gives the user a false sense that
@ -1097,7 +1098,7 @@ function callbackify(original) {
function callbackified(...args) { function callbackified(...args) {
const maybeCb = args.pop(); const maybeCb = args.pop();
if (typeof maybeCb !== 'function') { if (typeof maybeCb !== 'function') {
throw new ERR_INVALID_ARG_TYPE('last argument', 'Function'); throw new ERR_INVALID_ARG_TYPE('last argument', 'Function', maybeCb);
} }
const cb = (...args) => { Reflect.apply(maybeCb, this, args); }; const cb = (...args) => { Reflect.apply(maybeCb, this, args); };
// In true node style we process the callback on `nextTick` with all the // In true node style we process the callback on `nextTick` with all the

View File

@ -67,7 +67,7 @@ const heapSpaceStatisticsBuffer =
function setFlagsFromString(flags) { function setFlagsFromString(flags) {
if (typeof flags !== 'string') if (typeof flags !== 'string')
throw new ERR_INVALID_ARG_TYPE('flags', 'string'); throw new ERR_INVALID_ARG_TYPE('flags', 'string', flags);
_setFlagsFromString(flags); _setFlagsFromString(flags);
} }

View File

@ -127,7 +127,8 @@ function zlibBufferSync(engine, buffer) {
} else { } else {
throw new ERR_INVALID_ARG_TYPE( throw new ERR_INVALID_ARG_TYPE(
'buffer', 'buffer',
['string', 'Buffer', 'TypedArray', 'DataView', 'ArrayBuffer'] ['string', 'Buffer', 'TypedArray', 'DataView', 'ArrayBuffer'],
buffer
); );
} }
} }

View File

@ -24,7 +24,7 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "url" argument must be of type string' message: 'The "url" argument must be of type string. Received type number'
} }
); );
@ -33,7 +33,7 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "url" argument must be of type string' message: 'The "url" argument must be of type string. Received type number'
} }
); );
@ -42,7 +42,8 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "job" argument must be of type ModuleJob' message: 'The "job" argument must be of type ModuleJob. ' +
'Received type string'
} }
); );
@ -51,6 +52,6 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "url" argument must be of type string' message: 'The "url" argument must be of type string. Received type number'
} }
); );

View File

@ -317,10 +317,6 @@ try {
{ {
// Verify that throws() and doesNotThrow() throw on non-function block. // Verify that throws() and doesNotThrow() throw on non-function block.
function typeName(value) {
return value === null ? 'null' : typeof value;
}
const testBlockTypeError = (method, block) => { const testBlockTypeError = (method, block) => {
common.expectsError( common.expectsError(
() => method(block), () => method(block),
@ -328,7 +324,7 @@ try {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "block" argument must be of type Function. Received ' + message: 'The "block" argument must be of type Function. Received ' +
`type ${typeName(block)}` `type ${typeof block}`
} }
); );
}; };
@ -369,15 +365,15 @@ assert.throws(() => {
{ {
// Bad args to AssertionError constructor should throw TypeError. // Bad args to AssertionError constructor should throw TypeError.
const args = [1, true, false, '', null, Infinity, Symbol('test'), undefined]; const args = [1, true, false, '', null, Infinity, Symbol('test'), undefined];
const re = /^The "options" argument must be of type Object$/;
args.forEach((input) => { args.forEach((input) => {
assert.throws( assert.throws(
() => new assert.AssertionError(input), () => new assert.AssertionError(input),
common.expectsError({ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: re message: 'The "options" argument must be of type Object. ' +
})); `Received type ${typeof input}`
});
}); });
} }

View File

@ -28,15 +28,16 @@ assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0);
assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1); assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1);
assert.strictEqual(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1); assert.strictEqual(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1);
const errMsg = common.expectsError({ assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'), {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, message: 'The "buf2" argument must be one of type Buffer or Uint8Array. ' +
message: 'The "buf1", "buf2" arguments must be one of ' + 'Received type string'
'type Buffer or Uint8Array' });
}, 2); assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)), {
assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'), errMsg); code: 'ERR_INVALID_ARG_TYPE',
message: 'The "buf1" argument must be one of type Buffer or Uint8Array. ' +
assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)), errMsg); 'Received type string'
});
common.expectsError(() => Buffer.alloc(1).compare('abc'), { common.expectsError(() => Buffer.alloc(1).compare('abc'), {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',

View File

@ -44,23 +44,15 @@ assert.notStrictEqual(flatOne, one[0]);
assert.strictEqual(flatLong.toString(), check); assert.strictEqual(flatLong.toString(), check);
assert.strictEqual(flatLongLen.toString(), check); assert.strictEqual(flatLongLen.toString(), check);
assertWrongList(); [undefined, null, Buffer.from('hello')].forEach((value) => {
assertWrongList(null); assert.throws(() => {
assertWrongList(Buffer.from('hello'));
assertWrongList([42]);
assertWrongList(['hello', 'world']);
assertWrongList(['hello', Buffer.from('world')]);
function assertWrongList(value) {
common.expectsError(() => {
Buffer.concat(value); Buffer.concat(value);
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, message: 'The "list" argument must be one of type Array, Buffer, ' +
message: 'The "list" argument must be one of type ' + `or Uint8Array. Received type ${typeof value}`
'Array, Buffer, or Uint8Array'
}); });
} });
// eslint-disable-next-line node-core/crypto-check // eslint-disable-next-line node-core/crypto-check
const random10 = common.hasCrypto ? const random10 = common.hasCrypto ?

View File

@ -6,7 +6,7 @@ const { ChildProcess } = require('child_process');
assert.strictEqual(typeof ChildProcess, 'function'); assert.strictEqual(typeof ChildProcess, 'function');
function typeName(value) { function typeName(value) {
return value === null ? 'null' : typeof value; return typeof value;
} }
{ {

View File

@ -78,39 +78,26 @@ function stripLineEndings(obj) {
return obj.replace(/\n/g, ''); return obj.replace(/\n/g, '');
} }
// direct call Certificate() should return instance // Direct call Certificate() should return instance
assert(Certificate() instanceof Certificate); assert(Certificate() instanceof Certificate);
[1, {}, [], Infinity, true, 'test', undefined, null].forEach((i) => { [1, {}, [], Infinity, true, 'test', undefined, null].forEach((val) => {
common.expectsError( assert.throws(
() => Certificate.verifySpkac(i), () => Certificate.verifySpkac(val),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "spkac" argument must be one of type Buffer, TypedArray, ' + message: 'The "spkac" argument must be one of type Buffer, TypedArray, ' +
'or DataView' `or DataView. Received type ${typeof val}`
} }
); );
}); });
[1, {}, [], Infinity, true, undefined, null].forEach((i) => { [1, {}, [], Infinity, true, undefined, null].forEach((val) => {
common.expectsError( const errObj = {
() => Certificate.exportPublicKey(i), code: 'ERR_INVALID_ARG_TYPE',
{ message: 'The "spkac" argument must be one of type string, Buffer,' +
code: 'ERR_INVALID_ARG_TYPE', ` TypedArray, or DataView. Received type ${typeof val}`
type: TypeError, };
message: 'The "spkac" argument must be one of type string, Buffer,' + assert.throws(() => Certificate.exportPublicKey(val), errObj);
' TypedArray, or DataView' assert.throws(() => Certificate.exportChallenge(val), errObj);
}
);
common.expectsError(
() => Certificate.exportChallenge(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "spkac" argument must be one of type string, Buffer,' +
' TypedArray, or DataView'
}
);
}); });

View File

@ -81,7 +81,8 @@ testCipher2(Buffer.from('0123456789abcdef'));
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "cipher" argument must be of type string' message: 'The "cipher" argument must be of type string. ' +
'Received type object'
}); });
common.expectsError( common.expectsError(
@ -90,7 +91,7 @@ testCipher2(Buffer.from('0123456789abcdef'));
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "password" argument must be one of type string, Buffer, ' + message: 'The "password" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView' 'TypedArray, or DataView. Received type object'
}); });
common.expectsError( common.expectsError(
@ -99,7 +100,7 @@ testCipher2(Buffer.from('0123456789abcdef'));
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "data" argument must be one of type string, Buffer, ' + message: 'The "data" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView' 'TypedArray, or DataView. Received type object'
}); });
common.expectsError( common.expectsError(
@ -108,7 +109,7 @@ testCipher2(Buffer.from('0123456789abcdef'));
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "buffer" argument must be one of type Buffer, ' + message: 'The "buffer" argument must be one of type Buffer, ' +
'TypedArray, or DataView' 'TypedArray, or DataView. Received type object'
}); });
common.expectsError( common.expectsError(
@ -117,7 +118,7 @@ testCipher2(Buffer.from('0123456789abcdef'));
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "buffer" argument must be one of type Buffer, ' + message: 'The "buffer" argument must be one of type Buffer, ' +
'TypedArray, or DataView' 'TypedArray, or DataView. Received type object'
}); });
} }
@ -132,7 +133,8 @@ testCipher2(Buffer.from('0123456789abcdef'));
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "cipher" argument must be of type string' message: 'The "cipher" argument must be of type string. ' +
'Received type object'
}); });
common.expectsError( common.expectsError(
@ -141,7 +143,7 @@ testCipher2(Buffer.from('0123456789abcdef'));
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "password" argument must be one of type string, Buffer, ' + message: 'The "password" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView' 'TypedArray, or DataView. Received type object'
}); });
} }

View File

@ -92,7 +92,8 @@ function testCipher3(key, iv) {
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "cipher" argument must be of type string' message: 'The "cipher" argument must be of type string. ' +
'Received type object'
}); });
common.expectsError( common.expectsError(
@ -101,7 +102,7 @@ function testCipher3(key, iv) {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "key" argument must be one of type string, Buffer, ' + message: 'The "key" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView' 'TypedArray, or DataView. Received type object'
}); });
common.expectsError( common.expectsError(
@ -110,7 +111,7 @@ function testCipher3(key, iv) {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "iv" argument must be one of type string, Buffer, ' + message: 'The "iv" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView' 'TypedArray, or DataView. Received type number'
}); });
} }
@ -128,7 +129,8 @@ function testCipher3(key, iv) {
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "cipher" argument must be of type string' message: 'The "cipher" argument must be of type string. ' +
'Received type object'
}); });
common.expectsError( common.expectsError(
@ -137,7 +139,7 @@ function testCipher3(key, iv) {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "key" argument must be one of type string, Buffer, ' + message: 'The "key" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView' 'TypedArray, or DataView. Received type object'
}); });
common.expectsError( common.expectsError(
@ -146,7 +148,7 @@ function testCipher3(key, iv) {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "iv" argument must be one of type string, Buffer, ' + message: 'The "iv" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView' 'TypedArray, or DataView. Received type number'
}); });
} }

View File

@ -49,14 +49,14 @@ assert.strictEqual(dh2.verifyError, 0);
() => { }, () => { },
/abc/, /abc/,
{} {}
].forEach((i) => { ].forEach((input) => {
common.expectsError( common.expectsError(
() => crypto.createDiffieHellman(i), () => crypto.createDiffieHellman(input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "sizeOrKey" argument must be one of type number, string, ' + message: 'The "sizeOrKey" argument must be one of type number, string, ' +
'Buffer, TypedArray, or DataView' `Buffer, TypedArray, or DataView. Received type ${typeof input}`
} }
); );
}); });
@ -380,5 +380,6 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "curve" argument must be of type string' message: 'The "curve" argument must be of type string. ' +
'Received type undefined'
}); });

View File

@ -12,7 +12,7 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "id" argument must be of type string' message: 'The "id" argument must be of type string. Received type boolean'
}); });
common.expectsError( common.expectsError(
@ -20,7 +20,7 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "flags" argument must be of type number' message: 'The "flags" argument must be of type number. Received type string'
}); });
common.expectsError( common.expectsError(

View File

@ -152,7 +152,8 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "algorithm" argument must be of type string' message: 'The "algorithm" argument must be of type string. ' +
'Received type undefined'
} }
); );

View File

@ -18,7 +18,7 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "hmac" argument must be of type string' message: 'The "hmac" argument must be of type string. Received type object'
}); });
common.expectsError( common.expectsError(
@ -27,7 +27,7 @@ common.expectsError(
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "key" argument must be one of type string, TypedArray, or ' + message: 'The "key" argument must be one of type string, TypedArray, or ' +
'DataView' 'DataView. Received type object'
}); });
{ {

View File

@ -82,7 +82,8 @@ common.expectsError(
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "keylen" argument must be of type number' message: 'The "keylen" argument must be of type number. ' +
`Received type ${typeof notNumber}`
}); });
}); });
@ -107,7 +108,8 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "digest" argument must be one of type string or null' message: 'The "digest" argument must be one of type string or null. ' +
'Received type undefined'
}); });
common.expectsError( common.expectsError(
@ -115,58 +117,57 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "digest" argument must be one of type string or null' message: 'The "digest" argument must be one of type string or null. ' +
'Received type undefined'
}); });
[1, {}, [], true, undefined, null].forEach((i) => { [1, {}, [], true, undefined, null].forEach((input) => {
const msgPart2 = `Buffer, or TypedArray. Received type ${typeof input}`;
common.expectsError( common.expectsError(
() => crypto.pbkdf2(i, 'salt', 8, 8, 'sha256', common.mustNotCall()), () => crypto.pbkdf2(input, 'salt', 8, 8, 'sha256', common.mustNotCall()),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "password" argument must be one of type string, ' + message: `The "password" argument must be one of type string, ${msgPart2}`
'Buffer, or TypedArray'
} }
); );
common.expectsError( common.expectsError(
() => crypto.pbkdf2('pass', i, 8, 8, 'sha256', common.mustNotCall()), () => crypto.pbkdf2('pass', input, 8, 8, 'sha256', common.mustNotCall()),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "salt" argument must be one of type string, ' + message: `The "salt" argument must be one of type string, ${msgPart2}`
'Buffer, or TypedArray'
} }
); );
common.expectsError( common.expectsError(
() => crypto.pbkdf2Sync(i, 'salt', 8, 8, 'sha256'), () => crypto.pbkdf2Sync(input, 'salt', 8, 8, 'sha256'),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "password" argument must be one of type string, ' + message: `The "password" argument must be one of type string, ${msgPart2}`
'Buffer, or TypedArray'
} }
); );
common.expectsError( common.expectsError(
() => crypto.pbkdf2Sync('pass', i, 8, 8, 'sha256'), () => crypto.pbkdf2Sync('pass', input, 8, 8, 'sha256'),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "salt" argument must be one of type string, ' + message: `The "salt" argument must be one of type string, ${msgPart2}`
'Buffer, or TypedArray'
} }
); );
}); });
['test', {}, [], true, undefined, null].forEach((i) => { ['test', {}, [], true, undefined, null].forEach((i) => {
const received = `Received type ${typeof i}`;
common.expectsError( common.expectsError(
() => crypto.pbkdf2('pass', 'salt', i, 8, 'sha256', common.mustNotCall()), () => crypto.pbkdf2('pass', 'salt', i, 8, 'sha256', common.mustNotCall()),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "iterations" argument must be of type number' message: `The "iterations" argument must be of type number. ${received}`
} }
); );
@ -175,7 +176,7 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "iterations" argument must be of type number' message: `The "iterations" argument must be of type number. ${received}`
} }
); );
}); });

View File

@ -315,105 +315,51 @@ common.expectsError(
})); }));
} }
[1, [], {}, undefined, null, true, Infinity].forEach((i) => {
common.expectsError(
() => crypto.createSign(),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "algorithm" argument must be of type string'
}
);
common.expectsError(
() => crypto.createVerify(),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "algorithm" argument must be of type string'
}
);
});
{ {
const sign = crypto.createSign('SHA1'); const sign = crypto.createSign('SHA1');
const verify = crypto.createVerify('SHA1'); const verify = crypto.createVerify('SHA1');
[1, [], {}, undefined, null, true, Infinity].forEach((i) => { [1, [], {}, undefined, null, true, Infinity].forEach((input) => {
common.expectsError( const type = typeof input;
() => sign.update(i), const errObj = {
{ code: 'ERR_INVALID_ARG_TYPE',
code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError [ERR_INVALID_ARG_TYPE]',
type: TypeError, message: 'The "algorithm" argument must be of type string. ' +
message: 'The "data" argument must be one of type string, Buffer, ' + `Received type ${type}`
'TypedArray, or DataView' };
} assert.throws(() => crypto.createSign(input), errObj);
); assert.throws(() => crypto.createVerify(input), errObj);
common.expectsError(
() => verify.update(i), errObj.message = 'The "data" argument must be one of type string, ' +
{ `Buffer, TypedArray, or DataView. Received type ${type}`;
code: 'ERR_INVALID_ARG_TYPE', assert.throws(() => sign.update(input), errObj);
type: TypeError, assert.throws(() => verify.update(input), errObj);
message: 'The "data" argument must be one of type string, Buffer, ' + assert.throws(() => sign._write(input, 'utf8', () => {}), errObj);
'TypedArray, or DataView' assert.throws(() => verify._write(input, 'utf8', () => {}), errObj);
}
);
common.expectsError(
() => sign._write(i, 'utf8', () => {}),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "data" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView'
}
);
common.expectsError(
() => verify._write(i, 'utf8', () => {}),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "data" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView'
}
);
}); });
[ [
Uint8Array, Uint16Array, Uint32Array, Float32Array, Float64Array Uint8Array, Uint16Array, Uint32Array, Float32Array, Float64Array
].forEach((i) => { ].forEach((clazz) => {
// These should all just work // These should all just work
sign.update(new i()); sign.update(new clazz());
verify.update(new i()); verify.update(new clazz());
}); });
[1, {}, [], Infinity].forEach((i) => { [1, {}, [], Infinity].forEach((input) => {
common.expectsError( const type = typeof input;
() => sign.sign(i), const errObj = {
{ code: 'ERR_INVALID_ARG_TYPE',
code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError [ERR_INVALID_ARG_TYPE]',
type: TypeError, message: 'The "key" argument must be one of type string, Buffer, ' +
message: 'The "key" argument must be one of type string, Buffer, ' + `TypedArray, or DataView. Received type ${type}`
'TypedArray, or DataView' };
}
);
common.expectsError( assert.throws(() => sign.sign(input), errObj);
() => verify.verify(i), assert.throws(() => verify.verify(input), errObj);
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "key" argument must be one of type string, Buffer, ' +
'TypedArray, or DataView'
}
);
common.expectsError( errObj.message = 'The "signature" argument must be one of type string, ' +
() => verify.verify('test', i), `Buffer, TypedArray, or DataView. Received type ${type}`;
{ assert.throws(() => verify.verify('test', input), errObj);
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "signature" argument must be one of type string, ' +
'Buffer, TypedArray, or DataView'
}
);
}); });
} }

View File

@ -40,7 +40,8 @@ const dns = require('dns');
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "lookup" argument must be of type Function' message: 'The "lookup" argument must be of type Function. ' +
`Received type ${typeof value}`
}); });
}); });
} }

View File

@ -10,47 +10,27 @@ const onMessage = common.mustCall((err, bytes) => {
assert.strictEqual(bytes, buf.length); assert.strictEqual(bytes, buf.length);
}, 6); }, 6);
const expectedError = { code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message:
/^The "address" argument must be one of type string or falsy$/
};
const client = dgram.createSocket('udp4').bind(0, () => { const client = dgram.createSocket('udp4').bind(0, () => {
const port = client.address().port; const port = client.address().port;
// valid address: false // Check valid addresses
client.send(buf, port, false, onMessage); [false, '', null, 0, undefined].forEach((address) => {
client.send(buf, port, address, onMessage);
});
// valid address: empty string // Valid address: not provided
client.send(buf, port, '', onMessage);
// valid address: null
client.send(buf, port, null, onMessage);
// valid address: 0
client.send(buf, port, 0, onMessage);
// valid address: undefined
client.send(buf, port, undefined, onMessage);
// valid address: not provided
client.send(buf, port, onMessage); client.send(buf, port, onMessage);
// invalid address: object // Check invalid addresses
common.expectsError(() => { [[], 1, true].forEach((invalidInput) => {
client.send(buf, port, []); const expectedError = {
}, expectedError); code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
// invalid address: nonzero number message: 'The "address" argument must be one of type string or falsy. ' +
common.expectsError(() => { `Received type ${typeof invalidInput}`
client.send(buf, port, 1); };
}, expectedError); assert.throws(() => client.send(buf, port, invalidInput), expectedError);
});
// invalid address: true
common.expectsError(() => {
client.send(buf, port, true);
}, expectedError);
}); });
client.unref(); client.unref();

View File

@ -1,47 +1,37 @@
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const assert = require('assert');
const dgram = require('dgram'); const dgram = require('dgram');
const socket = dgram.createSocket('udp4'); const socket = dgram.createSocket('udp4');
const errorMessageOffset = const errObj = {
/^The "offset" argument must be of type number$/;
common.expectsError(() => {
socket.sendto();
}, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: errorMessageOffset message: 'The "offset" argument must be of type number. Received type ' +
}); 'undefined'
};
assert.throws(() => socket.sendto(), errObj);
common.expectsError(() => { errObj.message = 'The "length" argument must be of type number. Received ' +
socket.sendto('buffer', 1, 'offset', 'port', 'address', 'cb'); 'type string';
}, { assert.throws(
code: 'ERR_INVALID_ARG_TYPE', () => socket.sendto('buffer', 1, 'offset', 'port', 'address', 'cb'),
type: TypeError, errObj);
message: /^The "length" argument must be of type number$/
});
common.expectsError(() => { errObj.message = 'The "offset" argument must be of type number. Received ' +
socket.sendto('buffer', 'offset', 1, 'port', 'address', 'cb'); 'type string';
}, { assert.throws(
code: 'ERR_INVALID_ARG_TYPE', () => socket.sendto('buffer', 'offset', 1, 'port', 'address', 'cb'),
type: TypeError, errObj);
message: errorMessageOffset
});
common.expectsError(() => { errObj.message = 'The "address" argument must be of type string. Received ' +
socket.sendto('buffer', 1, 1, 10, false, 'cb'); 'type boolean';
}, { assert.throws(
code: 'ERR_INVALID_ARG_TYPE', () => socket.sendto('buffer', 1, 1, 10, false, 'cb'),
type: TypeError, errObj);
message: /^The "address" argument must be of type string$/
});
common.expectsError(() => { errObj.message = 'The "port" argument must be of type number. Received ' +
socket.sendto('buffer', 1, 1, false, 'address', 'cb'); 'type boolean';
}, { assert.throws(
code: 'ERR_INVALID_ARG_TYPE', () => socket.sendto('buffer', 1, 1, false, 'address', 'cb'),
type: TypeError, errObj);
message: /^The "port" argument must be of type number$/
});

View File

@ -92,5 +92,6 @@ common.expectsError(() => {
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "listener" argument must be of type Function' message: 'The "listener" argument must be of type Function. ' +
'Received type object'
}); });

View File

@ -52,12 +52,12 @@ e.emit('e');
// Verify that the listener must be a function // Verify that the listener must be a function
common.expectsError(() => { common.expectsError(() => {
const ee = new EventEmitter(); const ee = new EventEmitter();
ee.once('foo', null); ee.once('foo', null);
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "listener" argument must be of type Function' message: 'The "listener" argument must be of type Function. ' +
'Received type object'
}); });
{ {

View File

@ -21,12 +21,12 @@ myEE.emit('foo');
// Verify that the listener must be a function // Verify that the listener must be a function
common.expectsError(() => { common.expectsError(() => {
const ee = new EventEmitter(); const ee = new EventEmitter();
ee.prependOnceListener('foo', null); ee.prependOnceListener('foo', null);
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "listener" argument must be of type Function' message: 'The "listener" argument must be of type Function. ' +
'Received type object'
}); });
// Test fallback if prependListener is undefined. // Test fallback if prependListener is undefined.

View File

@ -146,12 +146,12 @@ function listener2() {}
// Verify that the removed listener must be a function // Verify that the removed listener must be a function
common.expectsError(() => { common.expectsError(() => {
const ee = new EventEmitter(); const ee = new EventEmitter();
ee.removeListener('foo', null); ee.removeListener('foo', null);
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "listener" argument must be of type Function' message: 'The "listener" argument must be of type Function. ' +
'Received type object'
}); });
{ {

View File

@ -97,7 +97,8 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "path" argument must be one of type string, Buffer, or URL' message: 'The "path" argument must be one of type string, Buffer, or URL.' +
' Received type number'
} }
); );

View File

@ -25,7 +25,8 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "path" argument must be one of type string, Buffer, or URL' message: 'The "path" argument must be one of type string, Buffer, or URL.' +
' Received type boolean'
} }
); );

View File

@ -146,40 +146,25 @@ if (fs.lchmod) {
})); }));
} }
['', false, null, undefined, {}, []].forEach((i) => { ['', false, null, undefined, {}, []].forEach((input) => {
common.expectsError( const errObj = {
() => fs.fchmod(i, 0o000), code: 'ERR_INVALID_ARG_TYPE',
{ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
code: 'ERR_INVALID_ARG_TYPE', message: 'The "fd" argument must be of type integer'
type: TypeError, };
message: 'The "fd" argument must be of type integer' assert.throws(() => fs.fchmod(input, 0o000), errObj);
} assert.throws(() => fs.fchmodSync(input, 0o000), errObj);
);
common.expectsError(
() => fs.fchmodSync(i, 0o000),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
}
);
}); });
[false, 1, {}, [], null, undefined].forEach((i) => { [false, 1, {}, [], null, undefined].forEach((input) => {
common.expectsError( const errObj = {
() => fs.chmod(i, 1, common.mustNotCall()), code: 'ERR_INVALID_ARG_TYPE',
{ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
code: 'ERR_INVALID_ARG_TYPE', message: 'The "path" argument must be one of type string, Buffer, or URL.' +
type: TypeError ` Received type ${typeof input}`
} };
); assert.throws(() => fs.chmod(input, 1, common.mustNotCall()), errObj);
common.expectsError( assert.throws(() => fs.chmodSync(input, 1), errObj);
() => fs.chmodSync(i, 1),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
}
);
}); });
process.on('exit', function() { process.on('exit', function() {

View File

@ -3,24 +3,16 @@
// This tests that the errors thrown from fs.close and fs.closeSync // This tests that the errors thrown from fs.close and fs.closeSync
// include the desired properties // include the desired properties
const common = require('../common'); require('../common');
const assert = require('assert');
const fs = require('fs'); const fs = require('fs');
['', false, null, undefined, {}, []].forEach((i) => { ['', false, null, undefined, {}, []].forEach((input) => {
common.expectsError( const errObj = {
() => fs.close(i), code: 'ERR_INVALID_ARG_TYPE',
{ name: 'TypeError [ERR_INVALID_ARG_TYPE]',
code: 'ERR_INVALID_ARG_TYPE', message: 'The "fd" argument must be of type integer'
type: TypeError, };
message: 'The "fd" argument must be of type integer' assert.throws(() => fs.close(input), errObj);
} assert.throws(() => fs.closeSync(input), errObj);
);
common.expectsError(
() => fs.closeSync(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
}
);
}); });

View File

@ -14,7 +14,8 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "buffer" argument must be one of type Buffer or Uint8Array' message: 'The "buffer" argument must be one of type Buffer or Uint8Array.' +
' Received type number'
} }
); );
@ -26,8 +27,11 @@ common.expectsError(
expected.length, expected.length,
0, 0,
common.mustNotCall()); common.mustNotCall());
}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, }, {
message: 'The "fd" argument must be of type integer' }); code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
});
}); });
common.expectsError(() => { common.expectsError(() => {
@ -56,7 +60,8 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "buffer" argument must be one of type Buffer or Uint8Array' message: 'The "buffer" argument must be one of type Buffer or Uint8Array.' +
' Received type number'
} }
); );
@ -67,8 +72,11 @@ common.expectsError(
0, 0,
expected.length, expected.length,
0); 0);
}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError, }, {
message: 'The "fd" argument must be of type integer' }); code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
});
}); });
common.expectsError(() => { common.expectsError(() => {

View File

@ -60,7 +60,8 @@ common.expectsError(
() => { fs.readFile(() => {}, common.mustNotCall()); }, () => { fs.readFile(() => {}, common.mustNotCall()); },
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
message: 'The "path" argument must be one of type string, Buffer, or URL', message: 'The "path" argument must be one of type string, Buffer, or URL.' +
' Received type function',
type: TypeError type: TypeError
} }
); );

View File

@ -1,43 +1,41 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const fs = require('fs'); const fs = require('fs');
[false, 1, [], {}, null, undefined].forEach((i) => { [false, 1, [], {}, null, undefined].forEach((input) => {
common.expectsError( const type = `of type string, Buffer, or URL. Received type ${typeof input}`;
() => fs.rename(i, 'does-not-exist', common.mustNotCall()), assert.throws(
() => fs.rename(input, 'does-not-exist', common.mustNotCall()),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: message: `The "oldPath" argument must be one ${type}`
'The "oldPath" argument must be one of type string, Buffer, or URL'
} }
); );
common.expectsError( assert.throws(
() => fs.rename('does-not-exist', i, common.mustNotCall()), () => fs.rename('does-not-exist', input, common.mustNotCall()),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: message: `The "newPath" argument must be one ${type}`
'The "newPath" argument must be one of type string, Buffer, or URL'
} }
); );
common.expectsError( assert.throws(
() => fs.renameSync(i, 'does-not-exist'), () => fs.renameSync(input, 'does-not-exist'),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: message: `The "oldPath" argument must be one ${type}`
'The "oldPath" argument must be one of type string, Buffer, or URL'
} }
); );
common.expectsError( assert.throws(
() => fs.renameSync('does-not-exist', i), () => fs.renameSync('does-not-exist', input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: message: `The "newPath" argument must be one ${type}`
'The "newPath" argument must be one of type string, Buffer, or URL'
} }
); );
}); });

View File

@ -131,52 +131,46 @@ fs.stat(__filename, common.mustCall(function(err, s) {
}); });
})); }));
['', false, null, undefined, {}, []].forEach((i) => { ['', false, null, undefined, {}, []].forEach((input) => {
common.expectsError( ['fstat', 'fstatSync'].forEach((fnName) => {
() => fs.fstat(i), assert.throws(
{ () => fs[fnName](input),
code: 'ERR_INVALID_ARG_TYPE', {
type: TypeError, code: 'ERR_INVALID_ARG_TYPE',
message: 'The "fd" argument must be of type integer' name: 'TypeError [ERR_INVALID_ARG_TYPE]',
} message: 'The "fd" argument must be of type integer'
); }
common.expectsError( );
() => fs.fstatSync(i), });
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
}
);
}); });
[false, 1, {}, [], null, undefined].forEach((i) => { [false, 1, {}, [], null, undefined].forEach((input) => {
common.expectsError( assert.throws(
() => fs.lstat(i, common.mustNotCall()), () => fs.lstat(input, common.mustNotCall()),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError name: 'TypeError [ERR_INVALID_ARG_TYPE]'
} }
); );
common.expectsError( assert.throws(
() => fs.lstatSync(i), () => fs.lstatSync(input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError name: 'TypeError [ERR_INVALID_ARG_TYPE]'
} }
); );
common.expectsError( assert.throws(
() => fs.stat(i, common.mustNotCall()), () => fs.stat(input, common.mustNotCall()),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError name: 'TypeError [ERR_INVALID_ARG_TYPE]'
} }
); );
common.expectsError( assert.throws(
() => fs.statSync(i), () => fs.statSync(input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError name: 'TypeError [ERR_INVALID_ARG_TYPE]'
} }
); );
}); });

View File

@ -182,12 +182,12 @@ function testFtruncate(cb) {
const fd = fs.openSync(file5, 'r+'); const fd = fs.openSync(file5, 'r+');
process.on('exit', () => fs.closeSync(fd)); process.on('exit', () => fs.closeSync(fd));
['', false, null, {}, []].forEach((i) => { ['', false, null, {}, []].forEach((input) => {
common.expectsError( assert.throws(
() => fs.ftruncate(fd, i), () => fs.ftruncate(fd, input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "len" argument must be of type integer' message: 'The "len" argument must be of type integer'
} }
); );
@ -210,21 +210,15 @@ function testFtruncate(cb) {
})); }));
} }
['', false, null, undefined, {}, []].forEach((i) => { ['', false, null, undefined, {}, []].forEach((input) => {
common.expectsError( ['ftruncate', 'ftruncateSync'].forEach((fnName) => {
() => fs.ftruncate(i), assert.throws(
{ () => fs[fnName](input),
code: 'ERR_INVALID_ARG_TYPE', {
type: TypeError, code: 'ERR_INVALID_ARG_TYPE',
message: 'The "fd" argument must be of type integer' name: 'TypeError [ERR_INVALID_ARG_TYPE]',
} message: 'The "fd" argument must be of type integer'
); }
common.expectsError( );
() => fs.ftruncateSync(i), });
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
}
);
}); });

View File

@ -79,14 +79,14 @@ for (const testCase of cases) {
}, 100); }, 100);
} }
[false, 1, {}, [], null, undefined].forEach((i) => { [false, 1, {}, [], null, undefined].forEach((input) => {
common.expectsError( common.expectsError(
() => fs.watch(i, common.mustNotCall()), () => fs.watch(input, common.mustNotCall()),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "filename" argument must be one of ' + message: 'The "filename" argument must be one of type string, Buffer, ' +
'type string, Buffer, or URL' `or URL. Received type ${typeof input}`
} }
); );
}); });

View File

@ -71,7 +71,8 @@ const s = http.createServer(common.mustCall((req, res) => {
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "name" argument must be of type string' message: 'The "name" argument must be of type string. ' +
'Received type undefined'
} }
); );
common.expectsError( common.expectsError(
@ -79,7 +80,8 @@ const s = http.createServer(common.mustCall((req, res) => {
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "name" argument must be of type string' message: 'The "name" argument must be of type string. ' +
'Received type undefined'
} }
); );
@ -122,7 +124,8 @@ const s = http.createServer(common.mustCall((req, res) => {
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "name" argument must be of type string' message: 'The "name" argument must be of type string. ' +
`Received type ${typeof val}`
} }
); );
}); });

View File

@ -79,7 +79,8 @@ common.expectsError(() => {
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The first argument must be one of type string or Buffer' message: 'The first argument must be one of type string or Buffer. ' +
'Received type undefined'
}); });
common.expectsError(() => { common.expectsError(() => {
@ -88,7 +89,8 @@ common.expectsError(() => {
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The first argument must be one of type string or Buffer' message: 'The first argument must be one of type string or Buffer. ' +
'Received type number'
}); });
// addTrailers() // addTrailers()

View File

@ -48,7 +48,8 @@ server.listen(0, common.mustCall(() => {
{ {
type: TypeError, type: TypeError,
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
message: 'The "id" argument must be of type number' message: 'The "id" argument must be of type number. Received type ' +
typeof value
} }
); );
}); });

View File

@ -53,11 +53,11 @@ server.listen(0, common.mustCall(function() {
message: 'The "method" argument must be of type string' message: 'The "method" argument must be of type string'
} }
); );
common.expectsError( assert.throws(
() => request.method = true, () => request.method = true,
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "method" argument must be of type string' message: 'The "method" argument must be of type string'
} }
); );

View File

@ -41,30 +41,17 @@ server.listen(0, common.mustCall(function() {
response.removeHeader(denormalised); response.removeHeader(denormalised);
assert.strictEqual(response.hasHeader(denormalised), false); assert.strictEqual(response.hasHeader(denormalised), false);
common.expectsError( ['hasHeader', 'getHeader', 'removeHeader'].forEach((fnName) => {
() => response.hasHeader(), assert.throws(
{ () => response[fnName](),
code: 'ERR_INVALID_ARG_TYPE', {
type: TypeError, code: 'ERR_INVALID_ARG_TYPE',
message: 'The "name" argument must be of type string' name: 'TypeError [ERR_INVALID_ARG_TYPE]',
} message: 'The "name" argument must be of type string. Received ' +
); 'type undefined'
common.expectsError( }
() => response.getHeader(), );
{ });
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "name" argument must be of type string'
}
);
common.expectsError(
() => response.removeHeader(),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "name" argument must be of type string'
}
);
[ [
':status', ':status',
@ -95,11 +82,12 @@ server.listen(0, common.mustCall(function() {
message: 'Invalid value "undefined" for header "foo-bar"' message: 'Invalid value "undefined" for header "foo-bar"'
}); });
common.expectsError( common.expectsError(
() => response.setHeader(), // header name undefined () => response.setHeader(), // Header name undefined
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "name" argument must be of type string' message: 'The "name" argument must be of type string. Received type ' +
'undefined'
} }
); );
common.expectsError( common.expectsError(

View File

@ -40,11 +40,12 @@ server.listen(0, common.mustCall(() => {
} }
); );
common.expectsError( common.expectsError(
() => response.setTrailer(), // trailer name undefined () => response.setTrailer(), // Trailer name undefined
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "name" argument must be of type string' message: 'The "name" argument must be of type string. Received type ' +
'undefined'
} }
); );
common.expectsError( common.expectsError(

View File

@ -4,19 +4,19 @@ const common = require('../common');
if (!common.hasCrypto) if (!common.hasCrypto)
common.skip('missing crypto'); common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2'); const http2 = require('http2');
const invalidOptions = [() => {}, 1, 'test', null];
const invalidArgTypeError = {
type: TypeError,
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type Object'
};
// Error if options are not passed to createSecureServer // Error if options are not passed to createSecureServer
const invalidOptions = [() => {}, 1, 'test', null];
invalidOptions.forEach((invalidOption) => { invalidOptions.forEach((invalidOption) => {
common.expectsError( assert.throws(
() => http2.createSecureServer(invalidOption), () => http2.createSecureServer(invalidOption),
invalidArgTypeError {
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type Object. Received ' +
`type ${typeof invalidOption}`
}
); );
}); });

View File

@ -99,14 +99,15 @@ http2.getPackedSettings({ enablePush: false });
0x00, 0x00, 0x00, 0x64, 0x00, 0x06, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x64, 0x00, 0x06, 0x00, 0x00, 0x00, 0x64,
0x00, 0x02, 0x00, 0x00, 0x00, 0x01]); 0x00, 0x02, 0x00, 0x00, 0x00, 0x01]);
[1, true, '', [], {}, NaN].forEach((i) => { [1, true, '', [], {}, NaN].forEach((input) => {
common.expectsError(() => { common.expectsError(() => {
http2.getUnpackedSettings(i); http2.getUnpackedSettings(input);
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: message:
'The "buf" argument must be one of type Buffer, TypedArray, or DataView' 'The "buf" argument must be one of type Buffer, TypedArray, or ' +
`DataView. Received type ${typeof input}`
}); });
}); });

View File

@ -13,7 +13,8 @@ server.on('stream', common.mustCall((stream) => {
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "code" argument must be of type number' message: 'The "code" argument must be of type number. ' +
'Received type string'
} }
); );
stream.respond(); stream.respond();

View File

@ -35,7 +35,7 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "test" argument must be of type Object' message: 'The "test" argument must be of type Object. Received type string'
}); });
common.expectsError( common.expectsError(
@ -43,7 +43,7 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "test" argument must be of type Date' message: 'The "test" argument must be of type Date. Received type string'
}); });
assertIsObject({}, 'test'); assertIsObject({}, 'test');

View File

@ -81,14 +81,14 @@ server.listen(0, common.mustCall(() => {
// should throw if payload is not of type ArrayBufferView // should throw if payload is not of type ArrayBufferView
{ {
[1, true, {}, []].forEach((invalidPayload) => [1, true, {}, []].forEach((payload) =>
common.expectsError( common.expectsError(
() => client.ping(invalidPayload), () => client.ping(payload),
{ {
type: TypeError, type: TypeError,
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
message: 'The "payload" argument must be one of type' + message: 'The "payload" argument must be one of type Buffer, ' +
' Buffer, TypedArray, or DataView' `TypedArray, or DataView. Received type ${typeof payload}`
} }
) )
); );

View File

@ -43,7 +43,8 @@ server.on('stream', common.mustCall((stream) => {
{ {
type: TypeError, type: TypeError,
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
message: 'The "fd" argument must be of type number' message: 'The "fd" argument must be of type number. Received type ' +
typeof types[type]
} }
); );
}); });

View File

@ -18,30 +18,32 @@ const types = [
server.on('stream', common.mustCall((stream) => { server.on('stream', common.mustCall((stream) => {
const session = stream.session; const session = stream.session;
types.forEach((i) => { types.forEach((input) => {
common.expectsError( common.expectsError(
() => session.goaway(i), () => session.goaway(input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "code" argument must be of type number' message: 'The "code" argument must be of type number. Received type ' +
typeof input
} }
); );
common.expectsError( common.expectsError(
() => session.goaway(0, i), () => session.goaway(0, input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "lastStreamID" argument must be of type number' message: 'The "lastStreamID" argument must be of type number. ' +
`Received type ${typeof input}`
} }
); );
common.expectsError( common.expectsError(
() => session.goaway(0, 0, i), () => session.goaway(0, 0, input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "opaqueData" argument must be one of type Buffer, ' + message: 'The "opaqueData" argument must be one of type Buffer, ' +
'TypedArray, or DataView' `TypedArray, or DataView. Received type ${typeof input}`
} }
); );
}); });

View File

@ -25,11 +25,12 @@ const {
Infinity, Infinity,
[], [],
[{}] [{}]
].forEach((i) => { ].forEach((input) => {
common.expectsError(() => assertIsObject(i, 'foo', 'Object'), common.expectsError(() => assertIsObject(input, 'foo', 'Object'),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
message: /^The "foo" argument must be of type Object$/ message: 'The "foo" argument must be of type Object. ' +
`Received type ${typeof input}`
}); });
}); });

View File

@ -6,6 +6,7 @@ const fixtures = require('../common/fixtures');
if (!common.hasCrypto) if (!common.hasCrypto)
common.skip('missing crypto'); common.skip('missing crypto');
const assert = require('assert');
const https = require('https'); const https = require('https');
function toArrayBuffer(buf) { function toArrayBuffer(buf) {
@ -38,8 +39,6 @@ const caArrBuff = toArrayBuffer(caCert);
const keyDataView = toDataView(keyBuff); const keyDataView = toDataView(keyBuff);
const certDataView = toDataView(certBuff); const certDataView = toDataView(certBuff);
const caArrDataView = toDataView(caCert); const caArrDataView = toDataView(caCert);
const invalidKeyRE = /^The "key" argument must be one of type string, Buffer, TypedArray, or DataView$/;
const invalidCertRE = /^The "cert" argument must be one of type string, Buffer, TypedArray, or DataView$/;
// Checks to ensure https.createServer doesn't throw an error // Checks to ensure https.createServer doesn't throw an error
// Format ['key', 'cert'] // Format ['key', 'cert']
@ -63,50 +62,59 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
[false, [certStr, certStr2]], [false, [certStr, certStr2]],
[[{ pem: keyBuff }], false], [[{ pem: keyBuff }], false],
[[{ pem: keyBuff }, { pem: keyBuff }], false] [[{ pem: keyBuff }, { pem: keyBuff }], false]
].forEach((params) => { ].forEach(([key, cert]) => {
https.createServer({ https.createServer({ key, cert });
key: params[0],
cert: params[1]
});
}); });
// Checks to ensure https.createServer predictably throws an error // Checks to ensure https.createServer predictably throws an error
// Format ['key', 'cert', 'expected message'] // Format ['key', 'cert', 'expected message']
[ [
[true, certBuff, invalidKeyRE], [true, certBuff],
[keyBuff, true, invalidCertRE], [true, certStr],
[true, certStr, invalidKeyRE], [true, certArrBuff],
[keyStr, true, invalidCertRE], [true, certDataView],
[true, certArrBuff, invalidKeyRE], [true, false],
[keyArrBuff, true, invalidCertRE], [true, false],
[true, certDataView, invalidKeyRE], [{ pem: keyBuff }, false, 'pem'],
[keyDataView, true, invalidCertRE], [1, false],
[true, true, invalidCertRE], [[keyBuff, true], [certBuff, certBuff2], 1],
[true, false, invalidKeyRE], [[true, keyStr2], [certStr, certStr2], 0],
[false, true, invalidCertRE], [[true, false], [certBuff, certBuff2], 0],
[true, false, invalidKeyRE], [true, [certBuff, certBuff2]]
[{ pem: keyBuff }, false, invalidKeyRE], ].forEach(([key, cert, index]) => {
[false, { pem: keyBuff }, invalidCertRE], const type = typeof (index === undefined ? key : key[index]);
[1, false, invalidKeyRE], assert.throws(() => {
[false, 1, invalidCertRE], https.createServer({ key, cert });
[[keyBuff, true], [certBuff, certBuff2], invalidKeyRE],
[[true, keyStr2], [certStr, certStr2], invalidKeyRE],
[[keyBuff, keyBuff2], [true, certBuff2], invalidCertRE],
[[keyStr, keyStr2], [certStr, true], invalidCertRE],
[[true, false], [certBuff, certBuff2], invalidKeyRE],
[[keyStr, keyStr2], [true, false], invalidCertRE],
[[keyStr, keyStr2], true, invalidCertRE],
[true, [certBuff, certBuff2], invalidKeyRE]
].forEach((params) => {
common.expectsError(() => {
https.createServer({
key: params[0],
cert: params[1]
});
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: params[2] message: 'The "key" argument must be one of type string, Buffer, ' +
`TypedArray, or DataView. Received type ${type}`
});
});
[
[keyBuff, true],
[keyStr, true],
[keyArrBuff, true],
[keyDataView, true],
[true, true],
[false, true],
[false, { pem: keyBuff }, 'pem'],
[false, 1],
[[keyBuff, keyBuff2], [true, certBuff2], 0],
[[keyStr, keyStr2], [certStr, true], 1],
[[keyStr, keyStr2], [true, false], 0],
[[keyStr, keyStr2], true],
].forEach(([key, cert, index]) => {
const type = typeof (index === undefined ? cert : cert[index]);
assert.throws(() => {
https.createServer({ key, cert });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "cert" argument must be one of type string, Buffer, ' +
`TypedArray, or DataView. Received type ${type}`
}); });
}); });
@ -120,12 +128,8 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
[keyBuff, certBuff, caArrBuff], [keyBuff, certBuff, caArrBuff],
[keyBuff, certBuff, caArrDataView], [keyBuff, certBuff, caArrDataView],
[keyBuff, certBuff, false], [keyBuff, certBuff, false],
].forEach((params) => { ].forEach(([key, cert, ca]) => {
https.createServer({ https.createServer({ key, cert, ca });
key: params[0],
cert: params[1],
ca: params[2]
});
}); });
// Checks to ensure https.createServer throws an error for CA assignment // Checks to ensure https.createServer throws an error for CA assignment
@ -135,17 +139,15 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
[keyBuff, certBuff, {}], [keyBuff, certBuff, {}],
[keyBuff, certBuff, 1], [keyBuff, certBuff, 1],
[keyBuff, certBuff, true], [keyBuff, certBuff, true],
[keyBuff, certBuff, [caCert, true]] [keyBuff, certBuff, [caCert, true], 1]
].forEach((params) => { ].forEach(([key, cert, ca, index]) => {
common.expectsError(() => { const type = typeof (index ? ca[index] : ca);
https.createServer({ assert.throws(() => {
key: params[0], https.createServer({ key, cert, ca });
cert: params[1],
ca: params[2]
});
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: /^The "ca" argument must be one of type string, Buffer, TypedArray, or DataView$/ message: 'The "ca" argument must be one of type string, Buffer, ' +
`TypedArray, or DataView. Received type ${type}`
}); });
}); });

View File

@ -47,7 +47,7 @@ common.expectsError(
type: TypeError, type: TypeError,
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
message: 'The "source" argument must be one of type Buffer ' + message: 'The "source" argument must be one of type Buffer ' +
'or Uint8Array. Received type null' 'or Uint8Array. Received type object'
} }
); );

View File

@ -90,36 +90,6 @@ common.expectsError(() => {
message: /.+ does not match \S/ message: /.+ does not match \S/
}); });
// // Test ERR_INVALID_ARG_TYPE
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b']),
'The "a" argument must be of type b');
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', ['b']]),
'The "a" argument must be of type b');
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', ['b', 'c']]),
'The "a" argument must be one of type b or c');
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE',
['a', ['b', 'c', 'd']]),
'The "a" argument must be one of type b, c, or d');
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b', 'c']),
'The "a" argument must be of type b. Received type string');
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE',
['a', 'b', undefined]),
'The "a" argument must be of type b. Received type ' +
'undefined');
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE',
['a', 'b', null]),
'The "a" argument must be of type b. Received type null');
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'not b']),
'The "a" argument must not be of type b');
assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a.b', 'not c']),
'The "a.b" property must not be of type c');
assert.strictEqual(
errors.message('ERR_INVALID_ARG_TYPE', ['first argument', 'c']),
'The first argument must be of type c');
assert.strictEqual(
errors.message('ERR_INVALID_ARG_TYPE', [['a', 'b', 'c'], 'not d']),
'The "a", "b", "c" arguments must not be of type d');
// Test ERR_INVALID_FD_TYPE // Test ERR_INVALID_FD_TYPE
assert.strictEqual(errors.message('ERR_INVALID_FD_TYPE', ['a']), assert.strictEqual(errors.message('ERR_INVALID_FD_TYPE', ['a']),
'Unsupported fd type: a'); 'Unsupported fd type: a');

View File

@ -62,16 +62,16 @@ tcp.listen(0, common.mustCall(function() {
undefined, undefined,
1, 1,
1.0, 1.0,
1 / 0,
+Infinity, +Infinity,
-Infinity, -Infinity,
[], [],
{} {}
].forEach((v) => { ].forEach((value) => {
common.expectsError(() => socket.write(v), { common.expectsError(() => socket.write(value), {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "chunk" argument must be one of type string or Buffer' message: 'The "chunk" argument must be one of type string or Buffer. ' +
`Received type ${typeof value}`
}); });
}); });

View File

@ -220,10 +220,6 @@ function checkFormat(path, testCases) {
assert.strictEqual(path.format(testCase[0]), testCase[1]); assert.strictEqual(path.format(testCase[0]), testCase[1]);
}); });
function typeName(value) {
return value === null ? 'null' : typeof value;
}
[null, undefined, 1, true, false, 'string'].forEach((pathObject) => { [null, undefined, 1, true, false, 'string'].forEach((pathObject) => {
common.expectsError(() => { common.expectsError(() => {
path.format(pathObject); path.format(pathObject);
@ -231,7 +227,7 @@ function checkFormat(path, testCases) {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "pathObject" argument must be of type Object. ' + message: 'The "pathObject" argument must be of type Object. ' +
`Received type ${typeName(pathObject)}` `Received type ${typeof pathObject}`
}); });
}); });
} }

View File

@ -70,12 +70,13 @@ const {
} }
{ {
[1, {}, [], null, undefined, Infinity].forEach((i) => { [1, {}, [], null, undefined, Infinity].forEach((input) => {
common.expectsError(() => performance.timerify(i), common.expectsError(() => performance.timerify(input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "fn" argument must be of type Function' message: 'The "fn" argument must be of type ' +
`Function. Received type ${typeof input}`
}); });
}); });
} }

View File

@ -37,14 +37,14 @@ assert.strictEqual(counts[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION], 0);
}); });
const observer = new PerformanceObserver(common.mustNotCall()); const observer = new PerformanceObserver(common.mustNotCall());
[1, null, undefined].forEach((i) => { [1, null, undefined].forEach((input) => {
//observer.observe(i);
common.expectsError( common.expectsError(
() => observer.observe(i), () => observer.observe(input),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "options" argument must be of type Object' message: 'The "options" argument must be of type Object. ' +
`Received type ${typeof input}`
}); });
}); });

View File

@ -6,7 +6,8 @@ common.expectsError(
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "fn" argument must be one of type Function or null' message: 'The "fn" argument must be one of type Function or null. ' +
'Received type number'
} }
); );

View File

@ -38,24 +38,14 @@ const assert = require('assert');
// //
// process.pid, String(process.pid): ourself // process.pid, String(process.pid): ourself
const invalidPidArgument = common.expectsError({ ['SIGTERM', null, undefined, NaN, Infinity, -Infinity].forEach((val) => {
code: 'ERR_INVALID_ARG_TYPE', assert.throws(() => process.kill(val), {
type: TypeError, code: 'ERR_INVALID_ARG_TYPE',
message: 'The "pid" argument must be of type number' name: 'TypeError [ERR_INVALID_ARG_TYPE]',
}, 6); message: 'The "pid" argument must be of type number. ' +
`Received type ${typeof val}`
assert.throws(function() { process.kill('SIGTERM'); }, });
invalidPidArgument); });
assert.throws(function() { process.kill(null); },
invalidPidArgument);
assert.throws(function() { process.kill(undefined); },
invalidPidArgument);
assert.throws(function() { process.kill(+'not a number'); },
invalidPidArgument);
assert.throws(function() { process.kill(1 / 0); },
invalidPidArgument);
assert.throws(function() { process.kill(-1 / 0); },
invalidPidArgument);
// Test that kill throws an error for unknown signal names // Test that kill throws an error for unknown signal names
common.expectsError(() => process.kill(0, 'test'), { common.expectsError(() => process.kill(0, 'test'), {

View File

@ -26,7 +26,8 @@ common.expectsError(() => tls.createServer({ handshakeTimeout: 'abcd' }),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "timeout" argument must be of type number' message: 'The "timeout" argument must ' +
'be of type number'
} }
); );

View File

@ -27,7 +27,8 @@ const client = connect({
client.setServername(value); client.setServername(value);
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
message: 'The "name" argument must be of type string' message: 'The "name" argument must be of type string. ' +
`Received type ${typeof value}`
}); });
}); });

View File

@ -43,7 +43,8 @@ common.expectsError(() => tls.createServer('this is not valid'),
{ {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "options" argument must be of type Object' message: 'The "options" argument must be of type ' +
'Object. Received type string'
} }
); );

View File

@ -38,8 +38,6 @@ const caArrBuff = toArrayBuffer(caCert);
const keyDataView = toDataView(keyBuff); const keyDataView = toDataView(keyBuff);
const certDataView = toDataView(certBuff); const certDataView = toDataView(certBuff);
const caArrDataView = toDataView(caCert); const caArrDataView = toDataView(caCert);
const invalidKeyRE = /^The "key" argument must be one of type string, Buffer, TypedArray, or DataView$/;
const invalidCertRE = /^The "cert" argument must be one of type string, Buffer, TypedArray, or DataView$/;
// Checks to ensure tls.createServer doesn't throw an error // Checks to ensure tls.createServer doesn't throw an error
// Format ['key', 'cert'] // Format ['key', 'cert']
@ -70,37 +68,51 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
// Checks to ensure tls.createServer predictably throws an error // Checks to ensure tls.createServer predictably throws an error
// Format ['key', 'cert', 'expected message'] // Format ['key', 'cert', 'expected message']
[ [
[true, certBuff, invalidKeyRE], [true, certBuff],
[keyBuff, true, invalidCertRE], [true, certStr],
[true, certStr, invalidKeyRE], [true, certArrBuff],
[keyStr, true, invalidCertRE], [true, certDataView],
[true, certArrBuff, invalidKeyRE], [true, false],
[keyArrBuff, true, invalidCertRE], [true, false],
[true, certDataView, invalidKeyRE], [{ pem: keyBuff }, false, 'pem'],
[keyDataView, true, invalidCertRE], [[keyBuff, true], [certBuff, certBuff2], 1],
[true, true, invalidCertRE], [[true, keyStr2], [certStr, certStr2], 0],
[true, false, invalidKeyRE], [[true, false], [certBuff, certBuff2], 0],
[false, true, invalidCertRE], [true, [certBuff, certBuff2]]
[true, false, invalidKeyRE], ].forEach(([key, cert, index]) => {
[{ pem: keyBuff }, false, invalidKeyRE], const type = typeof (index === undefined ? key : key[index]);
[false, { pem: keyBuff }, invalidCertRE],
[1, false, invalidKeyRE],
[false, 1, invalidCertRE],
[[keyBuff, true], [certBuff, certBuff2], invalidKeyRE],
[[true, keyStr2], [certStr, certStr2], invalidKeyRE],
[[keyBuff, keyBuff2], [true, certBuff2], invalidCertRE],
[[keyStr, keyStr2], [certStr, true], invalidCertRE],
[[true, false], [certBuff, certBuff2], invalidKeyRE],
[[keyStr, keyStr2], [true, false], invalidCertRE],
[[keyStr, keyStr2], true, invalidCertRE],
[true, [certBuff, certBuff2], invalidKeyRE]
].forEach(([key, cert, message]) => {
common.expectsError(() => { common.expectsError(() => {
tls.createServer({ key, cert }); tls.createServer({ key, cert });
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message message: 'The "key" argument must be one of type string, Buffer, ' +
`TypedArray, or DataView. Received type ${type}`
});
});
[
[keyBuff, true],
[keyStr, true],
[keyArrBuff, true],
[keyDataView, true],
[true, true],
[false, true],
[false, { pem: keyBuff }, 'pem'],
[false, 1],
[[keyBuff, keyBuff2], [true, certBuff2], 0],
[[keyStr, keyStr2], [certStr, true], 1],
[[keyStr, keyStr2], [true, false], 0],
[[keyStr, keyStr2], true]
].forEach(([key, cert, index]) => {
const type = typeof (index === undefined ? cert : cert[index]);
common.expectsError(() => {
tls.createServer({ key, cert });
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "cert" argument must be one of type string, Buffer, ' +
`TypedArray, or DataView. Received type ${type}`
}); });
}); });
@ -125,32 +137,16 @@ const invalidCertRE = /^The "cert" argument must be one of type string, Buffer,
[keyBuff, certBuff, {}], [keyBuff, certBuff, {}],
[keyBuff, certBuff, 1], [keyBuff, certBuff, 1],
[keyBuff, certBuff, true], [keyBuff, certBuff, true],
[keyBuff, certBuff, [caCert, true]] [keyBuff, certBuff, [caCert, true], 1]
].forEach(([key, cert, ca]) => { ].forEach(([key, cert, ca, index]) => {
const type = typeof (index === undefined ? ca : ca[index]);
common.expectsError(() => { common.expectsError(() => {
tls.createServer({ key, cert, ca }); tls.createServer({ key, cert, ca });
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: /^The "ca" argument must be one of type string, Buffer, TypedArray, or DataView$/ message: 'The "ca" argument must be one of type string, Buffer, ' +
}); `TypedArray, or DataView. Received type ${type}`
});
// Checks to ensure tls.createServer throws an error for CA assignment
// Format ['key', 'cert', 'ca']
[
[keyBuff, certBuff, true],
[keyBuff, certBuff, {}],
[keyBuff, certBuff, 1],
[keyBuff, certBuff, true],
[keyBuff, certBuff, [caCert, true]]
].forEach(([key, cert, ca]) => {
common.expectsError(() => {
tls.createServer({ key, cert, ca });
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "ca" argument must be one of type string, Buffer, TypedArray, or DataView$/
}); });
}); });

View File

@ -5,7 +5,7 @@ const url = require('url');
const throwsObjsAndReportTypes = new Map([ const throwsObjsAndReportTypes = new Map([
[undefined, 'undefined'], [undefined, 'undefined'],
[null, 'null'], [null, 'object'],
[true, 'boolean'], [true, 'boolean'],
[false, 'boolean'], [false, 'boolean'],
[0, 'number'], [0, 'number'],

View File

@ -21,15 +21,17 @@ assert.strictEqual(
); );
{ {
const expectedErr = common.expectsError({ [true, 1, 'test', Infinity].forEach((value) => {
code: 'ERR_INVALID_ARG_TYPE', assert.throws(
type: TypeError, () => url.format(myURL, value),
message: 'The "options" argument must be of type Object' {
}, 4); code: 'ERR_INVALID_ARG_TYPE',
assert.throws(() => url.format(myURL, true), expectedErr); name: 'TypeError [ERR_INVALID_ARG_TYPE]',
assert.throws(() => url.format(myURL, 1), expectedErr); message: 'The "options" argument must be of type Object. ' +
assert.throws(() => url.format(myURL, 'test'), expectedErr); `Received type ${typeof value}`
assert.throws(() => url.format(myURL, Infinity), expectedErr); }
);
});
} }
// Any falsy value other than undefined will be treated as false. // Any falsy value other than undefined will be treated as false.

View File

@ -6,7 +6,7 @@ const url = require('url');
// https://github.com/joyent/node/issues/568 // https://github.com/joyent/node/issues/568
[ [
[undefined, 'undefined'], [undefined, 'undefined'],
[null, 'null'], [null, 'object'],
[true, 'boolean'], [true, 'boolean'],
[false, 'boolean'], [false, 'boolean'],
[0.0, 'number'], [0.0, 'number'],

View File

@ -236,7 +236,8 @@ const values = [
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "original" argument must be of type Function' message: 'The "original" argument must be of type Function. ' +
`Received type ${typeof value}`
}); });
}); });
} }
@ -257,7 +258,8 @@ const values = [
}, { }, {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The last argument must be of type Function' message: 'The last argument must be of type Function. ' +
`Received type ${typeof value}`
}); });
}); });
} }

View File

@ -7,6 +7,7 @@ const util = require('util');
common.expectsError(() => util.deprecate(() => {}, 'message', notString), { common.expectsError(() => util.deprecate(() => {}, 'message', notString), {
code: 'ERR_INVALID_ARG_TYPE', code: 'ERR_INVALID_ARG_TYPE',
type: TypeError, type: TypeError,
message: 'The "code" argument must be of type string' message: 'The "code" argument must be of type string. ' +
`Received type ${typeof notString}`
}); });
}); });

Some files were not shown because too many files have changed in this diff Show More