Skip to content

Commit

Permalink
lib: always show ERR_INVALID_ARG_TYPE received part
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
BridgeAR committed Mar 25, 2018
1 parent eeb5702 commit c6b6c92
Show file tree
Hide file tree
Showing 107 changed files with 748 additions and 861 deletions.
9 changes: 5 additions & 4 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {

OutgoingMessage.prototype.getHeader = function getHeader(name) {
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;
Expand Down Expand Up @@ -576,7 +576,7 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {

OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
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()]);
Expand All @@ -585,7 +585,7 @@ OutgoingMessage.prototype.hasHeader = function hasHeader(name) {

OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string');
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
}

if (this._header) {
Expand Down Expand Up @@ -656,7 +656,8 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
}

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);
}


Expand Down
3 changes: 2 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ function chunkInvalid(state, chunk) {
typeof chunk !== 'string' &&
chunk !== undefined &&
!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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ function validChunk(stream, state, chunk, cb) {
if (chunk === null) {
er = new ERR_STREAM_NULL_VALUES();
} 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) {
stream.emit('error', er);
Expand Down
7 changes: 5 additions & 2 deletions lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ function SecureContext(secureProtocol, secureOptions, context) {
}

function validateKeyCert(value, type) {
if (typeof value !== 'string' && !isArrayBufferView(value))
if (typeof value !== 'string' && !isArrayBufferView(value)) {
throw new ERR_INVALID_ARG_TYPE(
// TODO(BridgeAR): Change this to `options.${type}`
type,
['string', 'Buffer', 'TypedArray', 'DataView']
['string', 'Buffer', 'TypedArray', 'DataView'],
value
);
}
}

exports.SecureContext = SecureContext;
Expand Down
4 changes: 2 additions & 2 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ TLSSocket.prototype._start = function() {

TLSSocket.prototype.setServername = function(name) {
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) {
Expand Down Expand Up @@ -877,7 +877,7 @@ function Server(options, listener) {
} else if (options == null || typeof options === 'object') {
options = options || {};
} else {
throw new ERR_INVALID_ARG_TYPE('options', 'Object');
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}


Expand Down
2 changes: 1 addition & 1 deletion lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function showEmitBeforeAfterWarning() {
class AsyncResource {
constructor(type, opts = {}) {
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') {
opts = { triggerAsyncId: opts, requireManualDestroy: false };
Expand Down
28 changes: 17 additions & 11 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,20 @@ Buffer.isBuffer = function isBuffer(b) {
return b instanceof Buffer;
};

Buffer.compare = function compare(a, b) {
if (!isUint8Array(a) || !isUint8Array(b)) {
throw new ERR_INVALID_ARG_TYPE(['buf1', 'buf2'], ['Buffer', 'Uint8Array']);
Buffer.compare = function compare(buf1, buf2) {
if (!isUint8Array(buf1)) {
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 _compare(a, b);
return _compare(buf1, buf2);
};

Buffer.isEncoding = function isEncoding(encoding) {
Expand All @@ -437,7 +441,8 @@ Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
Buffer.concat = function concat(list, length) {
var i;
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)
Expand Down Expand Up @@ -645,14 +650,15 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
return stringSlice(this, encoding, start, end);
};

Buffer.prototype.equals = function equals(b) {
if (!isUint8Array(b)) {
throw new ERR_INVALID_ARG_TYPE('otherBuffer', ['Buffer', 'Uint8Array'], b);
Buffer.prototype.equals = function equals(otherBuffer) {
if (!isUint8Array(otherBuffer)) {
throw new ERR_INVALID_ARG_TYPE(
'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
}
if (this === b)
if (this === otherBuffer)
return true;

return _compare(this, b) === 0;
return _compare(this, otherBuffer) === 0;
};

// Override how buffers are presented by util.inspect().
Expand Down
22 changes: 12 additions & 10 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function newHandle(type, lookup) {
if (lookup === undefined)
lookup = dns.lookup;
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') {
const handle = new UDP();
Expand Down Expand Up @@ -299,19 +299,19 @@ Socket.prototype.sendto = function(buffer,
address,
callback) {
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') {
throw new ERR_INVALID_ARG_TYPE('length', 'number');
throw new ERR_INVALID_ARG_TYPE('length', 'number', length);
}

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') {
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);
Expand All @@ -323,7 +323,7 @@ function sliceBuffer(buffer, offset, length) {
buffer = Buffer.from(buffer);
} else if (!isUint8Array(buffer)) {
throw new ERR_INVALID_ARG_TYPE('buffer',
['Buffer', 'Uint8Array', 'string']);
['Buffer', 'Uint8Array', 'string'], buffer);
}

offset = offset >>> 0;
Expand Down Expand Up @@ -415,13 +415,14 @@ Socket.prototype.send = function(buffer,
list = [ Buffer.from(buffer) ];
} else if (!isUint8Array(buffer)) {
throw new ERR_INVALID_ARG_TYPE('buffer',
['Buffer', 'Uint8Array', 'string']);
['Buffer', 'Uint8Array', 'string'],
buffer);
} else {
list = [ buffer ];
}
} else if (!(list = fixBufferList(buffer))) {
throw new ERR_INVALID_ARG_TYPE('buffer list arguments',
['Buffer', 'string']);
['Buffer', 'string'], buffer);
}

port = port >>> 0;
Expand All @@ -437,7 +438,7 @@ Socket.prototype.send = function(buffer,
callback = address;
address = undefined;
} 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();
Expand Down Expand Up @@ -602,7 +603,8 @@ Socket.prototype.setMulticastInterface = function(interfaceAddress) {
this._healthCheck();

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);
Expand Down
8 changes: 4 additions & 4 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function _addListener(target, type, listener, prepend) {

if (typeof listener !== 'function') {
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;
Expand Down Expand Up @@ -287,7 +287,7 @@ function _onceWrap(target, type, listener) {
EventEmitter.prototype.once = function once(type, listener) {
if (typeof listener !== 'function') {
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));
return this;
Expand All @@ -297,7 +297,7 @@ EventEmitter.prototype.prependOnceListener =
function prependOnceListener(type, listener) {
if (typeof listener !== 'function') {
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));
return this;
Expand All @@ -310,7 +310,7 @@ EventEmitter.prototype.removeListener =

if (typeof listener !== 'function') {
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;
Expand Down
2 changes: 1 addition & 1 deletion lib/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class FileHandle {

function validateFileHandle(handle) {
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) {
Expand Down
9 changes: 6 additions & 3 deletions lib/internal/crypto/certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function verifySpkac(spkac) {
if (!isArrayBufferView(spkac)) {
throw new ERR_INVALID_ARG_TYPE(
'spkac',
['Buffer', 'TypedArray', 'DataView']
['Buffer', 'TypedArray', 'DataView'],
spkac
);
}
return certVerifySpkac(spkac);
Expand All @@ -28,7 +29,8 @@ function exportPublicKey(spkac, encoding) {
if (!isArrayBufferView(spkac)) {
throw new ERR_INVALID_ARG_TYPE(
'spkac',
['string', 'Buffer', 'TypedArray', 'DataView']
['string', 'Buffer', 'TypedArray', 'DataView'],
spkac
);
}
return certExportPublicKey(spkac);
Expand All @@ -39,7 +41,8 @@ function exportChallenge(spkac, encoding) {
if (!isArrayBufferView(spkac)) {
throw new ERR_INVALID_ARG_TYPE(
'spkac',
['string', 'Buffer', 'TypedArray', 'DataView']
['string', 'Buffer', 'TypedArray', 'DataView'],
spkac
);
}
return certExportChallenge(spkac);
Expand Down
Loading

0 comments on commit c6b6c92

Please sign in to comment.