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

lib: improve the usage of TypeError[INVALID_ARG_TYPE] #16401

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function ClientRequest(options, cb) {
// when createConnection is provided.
} else if (typeof agent.addRequest !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'Agent option',
['Agent-like object', 'undefined', 'false']);
['Agent-like Object', 'undefined', 'false']);
}
this.agent = agent;

Expand Down
4 changes: 2 additions & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {

if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
['string', 'buffer']);
['string', 'Buffer']);
}


Expand Down Expand Up @@ -748,7 +748,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
if (chunk) {
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
['string', 'buffer']);
['string', 'Buffer']);
}
if (!this._header) {
if (typeof chunk === 'string')
Expand Down
2 changes: 1 addition & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ function Server(options, listener) {
} else if (options == null || typeof options === 'object') {
options = options || {};
} else {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
}


Expand Down
2 changes: 1 addition & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function innerThrows(shouldThrow, block, expected, message) {
var details = '';

if (typeof block !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'function',
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'Function',
block);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE',
'first argument',
['string', 'buffer', 'arrayBuffer', 'array', 'array-like object'],
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
value
);
}
Expand All @@ -226,7 +226,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE',
'first argument',
['string', 'buffer', 'arrayBuffer', 'array', 'array-like object'],
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
value
);
};
Expand Down Expand Up @@ -429,7 +429,7 @@ Buffer.isBuffer = function isBuffer(b) {
Buffer.compare = function compare(a, b) {
if (!isUint8Array(a) || !isUint8Array(b)) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', ['buf1', 'buf2'], ['buffer', 'uint8Array']
'ERR_INVALID_ARG_TYPE', ['buf1', 'buf2'], ['Buffer', 'Uint8Array']
);
}

Expand All @@ -448,7 +448,7 @@ Buffer.isEncoding = function isEncoding(encoding) {
Buffer[kIsEncodingSymbol] = Buffer.isEncoding;

const kConcatErr = new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'list', ['array', 'buffer', 'uint8Array']
'ERR_INVALID_ARG_TYPE', 'list', ['Array', 'Buffer', 'Uint8Array']
);

Buffer.concat = function concat(list, length) {
Expand Down Expand Up @@ -509,7 +509,7 @@ function byteLength(string, encoding) {

throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'string',
['string', 'buffer', 'arrayBuffer'], string
['string', 'Buffer', 'ArrayBuffer'], string
);
}

Expand Down Expand Up @@ -671,7 +671,7 @@ Buffer.prototype.equals = function equals(b) {
if (!isUint8Array(b)) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'otherBuffer',
['buffer', 'uint8Array'], b
['Buffer', 'Uint8Array'], b
);
}
if (this === b)
Expand Down Expand Up @@ -700,7 +700,7 @@ Buffer.prototype.compare = function compare(target,
if (!isUint8Array(target)) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'target',
['buffer', 'uint8Array'], target
['Buffer', 'Uint8Array'], target
);
}
if (arguments.length === 1)
Expand Down Expand Up @@ -783,7 +783,7 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {

throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'value',
['string', 'buffer', 'uint8Array'], val
['string', 'Buffer', 'Uint8Array'], val
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function newHandle(type, lookup) {
if (lookup === undefined)
lookup = dns.lookup;
else if (typeof lookup !== 'function')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'lookup', 'function');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'lookup', 'Function');

if (type === 'udp4') {
const handle = new UDP();
Expand Down
2 changes: 1 addition & 1 deletion lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function lookup(hostname, options, callback) {
// Parse arguments
if (hostname && typeof hostname !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'hostname',
['string', 'falsey'], hostname);
['string', 'falsy'], hostname);
} else if (typeof options === 'function') {
callback = options;
family = 0;
Expand Down
8 changes: 4 additions & 4 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function _addListener(target, type, listener, prepend) {

if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'function');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'Function');
}

events = target._events;
Expand Down Expand Up @@ -344,7 +344,7 @@ function _onceWrap(target, type, listener) {
EventEmitter.prototype.once = function once(type, listener) {
if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'function');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener', 'Function');
}
this.on(type, _onceWrap(this, type, listener));
return this;
Expand All @@ -355,7 +355,7 @@ EventEmitter.prototype.prependOnceListener =
if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener',
'function');
'Function');
}
this.prependListener(type, _onceWrap(this, type, listener));
return this;
Expand All @@ -369,7 +369,7 @@ EventEmitter.prototype.removeListener =
if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'listener',
'function');
'Function');
}

events = this._events;
Expand Down
8 changes: 4 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function getOptions(options, defaultOptions) {
} else if (typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'options',
['string', 'object'],
['string', 'Object'],
options);
}

Expand Down Expand Up @@ -1209,7 +1209,7 @@ function toUnixTimestamp(time) {
}
throw new errors.Error('ERR_INVALID_ARG_TYPE',
'time',
['Date', 'time in seconds'],
['Date', 'Time in seconds'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really need to be fixed in this PR, but this should be ['Date', 'string', 'number']

time);
}

Expand Down Expand Up @@ -1513,7 +1513,7 @@ fs.watchFile = function(filename, options, listener) {
if (typeof listener !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'listener',
'function',
'Function',
listener);
}

Expand Down Expand Up @@ -1922,7 +1922,7 @@ fs.copyFile = function(src, dest, flags, callback) {
callback = flags;
flags = 0;
} else if (typeof callback !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'callback', 'function');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'callback', 'Function');
}

src = getPathFromURL(src);
Expand Down
2 changes: 1 addition & 1 deletion lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Session extends EventEmitter {
}
if (params && typeof params !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'params', 'object', params);
'params', 'Object', params);
}
if (callback && typeof callback !== 'function') {
throw new errors.TypeError('ERR_INVALID_CALLBACK');
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ ChildProcess.prototype.spawn = function(options) {
var i;

if (options === null || typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object',
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object',
options);
}

Expand Down Expand Up @@ -594,7 +594,7 @@ function setupChannel(target, channel) {
options = undefined;
} else if (options !== undefined &&
(options === null || typeof options !== 'object')) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object',
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object',
options);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ function makeTextDecoderICU() {
constructor(encoding = 'utf-8', options = {}) {
encoding = `${encoding}`;
if (typeof options !== 'object')
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'options', 'object');
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'options', 'Object');

const enc = getEncodingFromLabel(encoding);
if (enc === undefined)
Expand Down Expand Up @@ -378,7 +378,7 @@ function makeTextDecoderICU() {
['ArrayBuffer', 'ArrayBufferView']);
}
if (typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
}

var flags = 0;
Expand Down Expand Up @@ -417,7 +417,7 @@ function makeTextDecoderJS() {
constructor(encoding = 'utf-8', options = {}) {
encoding = `${encoding}`;
if (typeof options !== 'object')
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'options', 'object');
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'options', 'Object');

const enc = getEncodingFromLabel(encoding);
if (enc === undefined || !hasConverter(enc))
Expand Down Expand Up @@ -452,7 +452,7 @@ function makeTextDecoderJS() {
['ArrayBuffer', 'ArrayBufferView']);
}
if (typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
}

if (this[kFlags] & CONVERTER_FLAGS_FLUSH) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class SystemError extends makeNodeError(Error) {
class AssertionError extends Error {
constructor(options) {
if (typeof options !== 'object' || options === null) {
throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
throw new exports.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
}
var { actual, expected, message, operator, stackStartFunction } = options;
if (message) {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2484,7 +2484,7 @@ function connect(authority, options, listener) {
if (typeof authority === 'string')
authority = new URL(authority);

assertIsObject(authority, 'authority', ['string', 'object', 'URL']);
assertIsObject(authority, 'authority', ['string', 'Object', 'URL']);

debug(`connecting to ${authority}`);

Expand Down Expand Up @@ -2539,7 +2539,7 @@ function createSecureServer(options, handler) {
if (options == null || typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'options',
'object');
'Object');
}
debug('creating http2secureserver');
return new Http2SecureServer(options, handler);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ class NghttpError extends Error {
}
}

function assertIsObject(value, name, types = 'object') {
function assertIsObject(value, name, types = 'Object') {
if (value !== undefined &&
(value === null ||
typeof value !== 'object' ||
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ function setup_cpuUsage() {
if (prevValue) {
if (!previousValueIsValid(prevValue.user)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'preValue.user', 'Number');
'preValue.user', 'number');
}

if (!previousValueIsValid(prevValue.system)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'preValue.system', 'Number');
'preValue.system', 'number');
}
}

Expand Down Expand Up @@ -154,7 +154,7 @@ function setupKillAndExit() {

// eslint-disable-next-line eqeqeq
if (pid != (pid | 0)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pid', 'Number');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pid', 'number');
}

// preserve null signal
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ Object.defineProperties(URL.prototype, {
// eslint-disable-next-line func-name-matching
value: function format(options) {
if (options && typeof options !== 'object')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
options = util._extend({
fragment: true,
unicode: false,
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,14 @@ const kCustomPromisifyArgsSymbol = Symbol('customPromisifyArgs');

function promisify(original) {
if (typeof original !== 'function')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'original', 'function');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'original', 'Function');

if (original[kCustomPromisifiedSymbol]) {
const fn = original[kCustomPromisifiedSymbol];
if (typeof fn !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'util.promisify.custom',
'function',
'Function',
fn);
}
Object.defineProperty(fn, kCustomPromisifiedSymbol, {
Expand Down
4 changes: 2 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ function lookupAndConnect(self, options) {
if (options.lookup && typeof options.lookup !== 'function')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'options.lookup',
'function',
'Function',
options.lookup);

var dnsopts = {
Expand Down Expand Up @@ -1225,7 +1225,7 @@ function Server(options, connectionListener) {
} else {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'options',
'object',
'Object',
options);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ REPLServer.prototype.defineCommand = function(keyword, cmd) {
} else if (typeof cmd.action !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
'action',
'function',
'Function',
cmd.action);
}
this.commands[keyword] = cmd;
Expand Down
2 changes: 1 addition & 1 deletion lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ function urlFormat(urlObject, options) {
urlObject = urlParse(urlObject);
} else if (typeof urlObject !== 'object' || urlObject === null) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'urlObject',
['object', 'string'], urlObject);
['Object', 'string'], urlObject);
} else if (!(urlObject instanceof Url)) {
var format = urlObject[formatSymbol];
return format ?
Expand Down
Loading