Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors: improve invalid args #19445

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ function ClientRequest(options, cb) {
// Explicitly pass through this statement as agent will not be used
// when createConnection is provided.
} else if (typeof agent.addRequest !== 'function') {
throw new ERR_INVALID_ARG_TYPE('Agent option',
['Agent-like Object', 'undefined', 'false']);
throw new ERR_INVALID_ARG_TYPE('options.agent',
['Agent-like Object', 'undefined', 'false'],
agent);
}
this.agent = agent;

Expand Down
11 changes: 6 additions & 5 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 Expand Up @@ -754,7 +755,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
var uncork;
if (chunk) {
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new ERR_INVALID_ARG_TYPE('first argument', ['string', 'Buffer']);
throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
}
if (!this._header) {
if (typeof chunk === 'string')
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
7 changes: 4 additions & 3 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 Expand Up @@ -908,7 +908,8 @@ function Server(options, listener) {
this[kSNICallback] = options.SNICallback;

if (typeof this[kHandshakeTimeout] !== 'number') {
throw new ERR_INVALID_ARG_TYPE('timeout', 'number');
throw new ERR_INVALID_ARG_TYPE(
'options.handshakeTimeout', 'number', options.handshakeTimeout);
}

if (this.sessionTimeout) {
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
33 changes: 21 additions & 12 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 All @@ -456,7 +461,10 @@ Buffer.concat = function concat(list, length) {
for (i = 0; i < list.length; i++) {
var buf = list[i];
if (!isUint8Array(buf)) {
throw new ERR_INVALID_ARG_TYPE('list', ['Array', 'Buffer', 'Uint8Array']);
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
// Instead, find the proper error code for this.
throw new ERR_INVALID_ARG_TYPE(
`list[${i}]`, ['Array', 'Buffer', 'Uint8Array'], list[i]);
}
_copy(buf, buffer, pos);
pos += buf.length;
Expand Down Expand Up @@ -645,14 +653,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
9 changes: 7 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,10 @@ fs.ftruncate = function(fd, len = 0, callback) {
len = 0;
}
validateUint32(fd, 'fd');
// TODO(BridgeAR): This does not seem right.
// There does not seem to be any validation before and if there is any, it
// should work similar to validateUint32 or not have a upper cap at all.
// This applies to all usage of `validateLen`.
validateLen(len);
len = Math.max(0, len);
const req = new FSReqWrap();
Expand Down Expand Up @@ -1012,7 +1016,7 @@ fs.fchmod = function(fd, mode, callback) {
mode = modeNum(mode);
validateUint32(fd, 'fd');
validateUint32(mode, 'mode');
// values for mode < 0 are already checked via the validateUint32 function
// Values for mode < 0 are already checked via the validateUint32 function
if (mode > 0o777)
throw new ERR_OUT_OF_RANGE('mode');

Expand All @@ -1025,7 +1029,8 @@ fs.fchmodSync = function(fd, mode) {
mode = modeNum(mode);
validateUint32(fd, 'fd');
validateUint32(mode, 'mode');
if (mode < 0 || mode > 0o777)
// Values for mode < 0 are already checked via the validateUint32 function
if (mode > 0o777)
throw new ERR_OUT_OF_RANGE('mode');
const ctx = {};
binding.fchmod(fd, mode, undefined, ctx);
Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -364,7 +364,7 @@ async function fchmod(handle, mode) {
mode = modeNum(mode);
validateFileHandle(handle);
validateUint32(mode, 'mode');
if (mode < 0 || mode > 0o777)
if (mode > 0o777)
throw new ERR_OUT_OF_RANGE('mode');
return binding.fchmod(handle.fd, mode, kUsePromises);
}
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