Skip to content

Commit c6b6c92

Browse files
committed
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: #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>
1 parent eeb5702 commit c6b6c92

File tree

107 files changed

+748
-861
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+748
-861
lines changed

lib/_http_outgoing.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
540540

541541
OutgoingMessage.prototype.getHeader = function getHeader(name) {
542542
if (typeof name !== 'string') {
543-
throw new ERR_INVALID_ARG_TYPE('name', 'string');
543+
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
544544
}
545545

546546
if (!this[outHeadersKey]) return;
@@ -576,7 +576,7 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
576576

577577
OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
578578
if (typeof name !== 'string') {
579-
throw new ERR_INVALID_ARG_TYPE('name', 'string');
579+
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
580580
}
581581

582582
return !!(this[outHeadersKey] && this[outHeadersKey][name.toLowerCase()]);
@@ -585,7 +585,7 @@ OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
585585

586586
OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
587587
if (typeof name !== 'string') {
588-
throw new ERR_INVALID_ARG_TYPE('name', 'string');
588+
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
589589
}
590590

591591
if (this._header) {
@@ -656,7 +656,8 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
656656
}
657657

658658
if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
659-
throw new ERR_INVALID_ARG_TYPE('first argument', ['string', 'Buffer']);
659+
throw new ERR_INVALID_ARG_TYPE('first argument',
660+
['string', 'Buffer'], chunk);
660661
}
661662

662663

lib/_stream_readable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ function chunkInvalid(state, chunk) {
294294
typeof chunk !== 'string' &&
295295
chunk !== undefined &&
296296
!state.objectMode) {
297-
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer', 'Uint8Array']);
297+
er = new ERR_INVALID_ARG_TYPE(
298+
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
298299
}
299300
return er;
300301
}

lib/_stream_writable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ function validChunk(stream, state, chunk, cb) {
255255
if (chunk === null) {
256256
er = new ERR_STREAM_NULL_VALUES();
257257
} else if (typeof chunk !== 'string' && !state.objectMode) {
258-
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer']);
258+
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
259259
}
260260
if (er) {
261261
stream.emit('error', er);

lib/_tls_common.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ function SecureContext(secureProtocol, secureOptions, context) {
5858
}
5959

6060
function validateKeyCert(value, type) {
61-
if (typeof value !== 'string' && !isArrayBufferView(value))
61+
if (typeof value !== 'string' && !isArrayBufferView(value)) {
6262
throw new ERR_INVALID_ARG_TYPE(
63+
// TODO(BridgeAR): Change this to `options.${type}`
6364
type,
64-
['string', 'Buffer', 'TypedArray', 'DataView']
65+
['string', 'Buffer', 'TypedArray', 'DataView'],
66+
value
6567
);
68+
}
6669
}
6770

6871
exports.SecureContext = SecureContext;

lib/_tls_wrap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ TLSSocket.prototype._start = function() {
667667

668668
TLSSocket.prototype.setServername = function(name) {
669669
if (typeof name !== 'string') {
670-
throw new ERR_INVALID_ARG_TYPE('name', 'string');
670+
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
671671
}
672672

673673
if (this._tlsOptions.isServer) {
@@ -877,7 +877,7 @@ function Server(options, listener) {
877877
} else if (options == null || typeof options === 'object') {
878878
options = options || {};
879879
} else {
880-
throw new ERR_INVALID_ARG_TYPE('options', 'Object');
880+
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
881881
}
882882

883883

lib/async_hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function showEmitBeforeAfterWarning() {
141141
class AsyncResource {
142142
constructor(type, opts = {}) {
143143
if (typeof type !== 'string')
144-
throw new ERR_INVALID_ARG_TYPE('type', 'string');
144+
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
145145

146146
if (typeof opts === 'number') {
147147
opts = { triggerAsyncId: opts, requireManualDestroy: false };

lib/buffer.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,20 @@ Buffer.isBuffer = function isBuffer(b) {
416416
return b instanceof Buffer;
417417
};
418418

419-
Buffer.compare = function compare(a, b) {
420-
if (!isUint8Array(a) || !isUint8Array(b)) {
421-
throw new ERR_INVALID_ARG_TYPE(['buf1', 'buf2'], ['Buffer', 'Uint8Array']);
419+
Buffer.compare = function compare(buf1, buf2) {
420+
if (!isUint8Array(buf1)) {
421+
throw new ERR_INVALID_ARG_TYPE('buf1', ['Buffer', 'Uint8Array'], buf1);
422422
}
423423

424-
if (a === b) {
424+
if (!isUint8Array(buf2)) {
425+
throw new ERR_INVALID_ARG_TYPE('buf2', ['Buffer', 'Uint8Array'], buf2);
426+
}
427+
428+
if (buf1 === buf2) {
425429
return 0;
426430
}
427431

428-
return _compare(a, b);
432+
return _compare(buf1, buf2);
429433
};
430434

431435
Buffer.isEncoding = function isEncoding(encoding) {
@@ -437,7 +441,8 @@ Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
437441
Buffer.concat = function concat(list, length) {
438442
var i;
439443
if (!Array.isArray(list)) {
440-
throw new ERR_INVALID_ARG_TYPE('list', ['Array', 'Buffer', 'Uint8Array']);
444+
throw new ERR_INVALID_ARG_TYPE(
445+
'list', ['Array', 'Buffer', 'Uint8Array'], list);
441446
}
442447

443448
if (list.length === 0)
@@ -645,14 +650,15 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
645650
return stringSlice(this, encoding, start, end);
646651
};
647652

648-
Buffer.prototype.equals = function equals(b) {
649-
if (!isUint8Array(b)) {
650-
throw new ERR_INVALID_ARG_TYPE('otherBuffer', ['Buffer', 'Uint8Array'], b);
653+
Buffer.prototype.equals = function equals(otherBuffer) {
654+
if (!isUint8Array(otherBuffer)) {
655+
throw new ERR_INVALID_ARG_TYPE(
656+
'otherBuffer', ['Buffer', 'Uint8Array'], otherBuffer);
651657
}
652-
if (this === b)
658+
if (this === otherBuffer)
653659
return true;
654660

655-
return _compare(this, b) === 0;
661+
return _compare(this, otherBuffer) === 0;
656662
};
657663

658664
// Override how buffers are presented by util.inspect().

lib/dgram.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function newHandle(type, lookup) {
7575
if (lookup === undefined)
7676
lookup = dns.lookup;
7777
else if (typeof lookup !== 'function')
78-
throw new ERR_INVALID_ARG_TYPE('lookup', 'Function');
78+
throw new ERR_INVALID_ARG_TYPE('lookup', 'Function', lookup);
7979

8080
if (type === 'udp4') {
8181
const handle = new UDP();
@@ -299,19 +299,19 @@ Socket.prototype.sendto = function(buffer,
299299
address,
300300
callback) {
301301
if (typeof offset !== 'number') {
302-
throw new ERR_INVALID_ARG_TYPE('offset', 'number');
302+
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
303303
}
304304

305305
if (typeof length !== 'number') {
306-
throw new ERR_INVALID_ARG_TYPE('length', 'number');
306+
throw new ERR_INVALID_ARG_TYPE('length', 'number', length);
307307
}
308308

309309
if (typeof port !== 'number') {
310-
throw new ERR_INVALID_ARG_TYPE('port', 'number');
310+
throw new ERR_INVALID_ARG_TYPE('port', 'number', port);
311311
}
312312

313313
if (typeof address !== 'string') {
314-
throw new ERR_INVALID_ARG_TYPE('address', 'string');
314+
throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
315315
}
316316

317317
this.send(buffer, offset, length, port, address, callback);
@@ -323,7 +323,7 @@ function sliceBuffer(buffer, offset, length) {
323323
buffer = Buffer.from(buffer);
324324
} else if (!isUint8Array(buffer)) {
325325
throw new ERR_INVALID_ARG_TYPE('buffer',
326-
['Buffer', 'Uint8Array', 'string']);
326+
['Buffer', 'Uint8Array', 'string'], buffer);
327327
}
328328

329329
offset = offset >>> 0;
@@ -415,13 +415,14 @@ Socket.prototype.send = function(buffer,
415415
list = [ Buffer.from(buffer) ];
416416
} else if (!isUint8Array(buffer)) {
417417
throw new ERR_INVALID_ARG_TYPE('buffer',
418-
['Buffer', 'Uint8Array', 'string']);
418+
['Buffer', 'Uint8Array', 'string'],
419+
buffer);
419420
} else {
420421
list = [ buffer ];
421422
}
422423
} else if (!(list = fixBufferList(buffer))) {
423424
throw new ERR_INVALID_ARG_TYPE('buffer list arguments',
424-
['Buffer', 'string']);
425+
['Buffer', 'string'], buffer);
425426
}
426427

427428
port = port >>> 0;
@@ -437,7 +438,7 @@ Socket.prototype.send = function(buffer,
437438
callback = address;
438439
address = undefined;
439440
} else if (address && typeof address !== 'string') {
440-
throw new ERR_INVALID_ARG_TYPE('address', ['string', 'falsy']);
441+
throw new ERR_INVALID_ARG_TYPE('address', ['string', 'falsy'], address);
441442
}
442443

443444
this._healthCheck();
@@ -602,7 +603,8 @@ Socket.prototype.setMulticastInterface = function(interfaceAddress) {
602603
this._healthCheck();
603604

604605
if (typeof interfaceAddress !== 'string') {
605-
throw new ERR_INVALID_ARG_TYPE('interfaceAddress', 'string');
606+
throw new ERR_INVALID_ARG_TYPE(
607+
'interfaceAddress', 'string', interfaceAddress);
606608
}
607609

608610
const err = this._handle.setMulticastInterface(interfaceAddress);

lib/events.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function _addListener(target, type, listener, prepend) {
197197

198198
if (typeof listener !== 'function') {
199199
const errors = lazyErrors();
200-
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function');
200+
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
201201
}
202202

203203
events = target._events;
@@ -287,7 +287,7 @@ function _onceWrap(target, type, listener) {
287287
EventEmitter.prototype.once = function once(type, listener) {
288288
if (typeof listener !== 'function') {
289289
const errors = lazyErrors();
290-
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function');
290+
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
291291
}
292292
this.on(type, _onceWrap(this, type, listener));
293293
return this;
@@ -297,7 +297,7 @@ EventEmitter.prototype.prependOnceListener =
297297
function prependOnceListener(type, listener) {
298298
if (typeof listener !== 'function') {
299299
const errors = lazyErrors();
300-
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function');
300+
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
301301
}
302302
this.prependListener(type, _onceWrap(this, type, listener));
303303
return this;
@@ -310,7 +310,7 @@ EventEmitter.prototype.removeListener =
310310

311311
if (typeof listener !== 'function') {
312312
const errors = lazyErrors();
313-
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function');
313+
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
314314
}
315315

316316
events = this._events;

lib/fs/promises.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class FileHandle {
112112

113113
function validateFileHandle(handle) {
114114
if (!(handle instanceof FileHandle))
115-
throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle');
115+
throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle', handle);
116116
}
117117

118118
async function writeFileHandle(filehandle, data, options) {

0 commit comments

Comments
 (0)