Skip to content

Commit b38c81c

Browse files
committed
lib: improve error handling
This improves the error handling for a couple cases where the received value would not have been handled so far or where the name is wrong etc. 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 acc3c77 commit b38c81c

9 files changed

+43
-16
lines changed

lib/_http_client.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ function ClientRequest(options, cb) {
8787
// Explicitly pass through this statement as agent will not be used
8888
// when createConnection is provided.
8989
} else if (typeof agent.addRequest !== 'function') {
90-
throw new ERR_INVALID_ARG_TYPE('Agent option',
91-
['Agent-like Object', 'undefined', 'false']);
90+
throw new ERR_INVALID_ARG_TYPE('options.agent',
91+
['Agent-like Object', 'undefined', 'false'],
92+
agent);
9293
}
9394
this.agent = agent;
9495

lib/_http_outgoing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
755755
var uncork;
756756
if (chunk) {
757757
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
758-
throw new ERR_INVALID_ARG_TYPE('first argument', ['string', 'Buffer']);
758+
throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
759759
}
760760
if (!this._header) {
761761
if (typeof chunk === 'string')

lib/_tls_wrap.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,8 @@ function Server(options, listener) {
908908
this[kSNICallback] = options.SNICallback;
909909

910910
if (typeof this[kHandshakeTimeout] !== 'number') {
911-
throw new ERR_INVALID_ARG_TYPE('timeout', 'number');
911+
throw new ERR_INVALID_ARG_TYPE(
912+
'options.handshakeTimeout', 'number', options.handshakeTimeout);
912913
}
913914

914915
if (this.sessionTimeout) {

lib/buffer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,10 @@ Buffer.concat = function concat(list, length) {
461461
for (i = 0; i < list.length; i++) {
462462
var buf = list[i];
463463
if (!isUint8Array(buf)) {
464-
throw new ERR_INVALID_ARG_TYPE('list', ['Array', 'Buffer', 'Uint8Array']);
464+
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
465+
// Instead, find the proper error code for this.
466+
throw new ERR_INVALID_ARG_TYPE(
467+
`list[${i}]`, ['Array', 'Buffer', 'Uint8Array'], list[i]);
465468
}
466469
_copy(buf, buffer, pos);
467470
pos += buf.length;

lib/internal/http2/compat.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED,
1414
ERR_HTTP2_STATUS_INVALID,
1515
ERR_INVALID_ARG_TYPE,
16+
ERR_INVALID_ARG_VALUE,
1617
ERR_INVALID_CALLBACK,
1718
ERR_INVALID_HTTP_TOKEN
1819
} = require('internal/errors').codes;
@@ -312,8 +313,10 @@ class Http2ServerRequest extends Readable {
312313
}
313314

314315
set method(method) {
315-
if (typeof method !== 'string' || method.trim() === '')
316-
throw new ERR_INVALID_ARG_TYPE('method', 'string');
316+
if (typeof method !== 'string')
317+
throw new ERR_INVALID_ARG_TYPE('method', 'string', method);
318+
if (method.trim() === '')
319+
throw new ERR_INVALID_ARG_VALUE('method', method);
317320

318321
this[kHeaders][HTTP2_HEADER_METHOD] = method;
319322
}

test/parallel/test-buffer-concat.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ assert.strictEqual(flatLongLen.toString(), check);
5454
});
5555
});
5656

57+
[[42], ['hello', Buffer.from('world')]].forEach((value) => {
58+
assert.throws(() => {
59+
Buffer.concat(value);
60+
}, {
61+
code: 'ERR_INVALID_ARG_TYPE',
62+
message: 'The "list[0]" argument must be one of type Array, Buffer, ' +
63+
`or Uint8Array. Received type ${typeof value[0]}`
64+
});
65+
});
66+
67+
assert.throws(() => {
68+
Buffer.concat([Buffer.from('hello'), 3]);
69+
}, {
70+
code: 'ERR_INVALID_ARG_TYPE',
71+
message: 'The "list[1]" argument must be one of type Array, Buffer, ' +
72+
'or Uint8Array. Received type number'
73+
});
74+
5775
// eslint-disable-next-line node-core/crypto-check
5876
const random10 = common.hasCrypto ?
5977
require('crypto').randomBytes(10) :

test/parallel/test-http-client-reject-unexpected-agent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ server.listen(0, baseOptions.host, common.mustCall(function() {
5252
{
5353
code: 'ERR_INVALID_ARG_TYPE',
5454
type: TypeError,
55-
message: 'The "Agent option" argument must be one of type ' +
56-
'Agent-like Object, undefined, or false'
55+
message: 'The "options.agent" property must be one of type Agent-like' +
56+
` Object, undefined, or false. Received type ${typeof agent}`
5757
}
5858
);
5959
});

test/parallel/test-http2-compat-serverrequest-headers.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ server.listen(0, common.mustCall(function() {
4545
// change the request method
4646
request.method = 'POST';
4747
assert.strictEqual(request.method, 'POST');
48-
common.expectsError(
48+
assert.throws(
4949
() => request.method = ' ',
5050
{
51-
code: 'ERR_INVALID_ARG_TYPE',
52-
type: TypeError,
53-
message: 'The "method" argument must be of type string'
51+
code: 'ERR_INVALID_ARG_VALUE',
52+
name: 'TypeError [ERR_INVALID_ARG_VALUE]',
53+
message: "The argument 'method' is invalid. Received ' '"
5454
}
5555
);
5656
assert.throws(
5757
() => request.method = true,
5858
{
5959
code: 'ERR_INVALID_ARG_TYPE',
6060
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
61-
message: 'The "method" argument must be of type string'
61+
message: 'The "method" argument must be of type string. ' +
62+
'Received type boolean'
6263
}
6364
);
6465

test/parallel/test-tls-basic-validations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ common.expectsError(() => tls.createServer({ handshakeTimeout: 'abcd' }),
2626
{
2727
code: 'ERR_INVALID_ARG_TYPE',
2828
type: TypeError,
29-
message: 'The "timeout" argument must ' +
30-
'be of type number'
29+
message: 'The "options.handshakeTimeout" property must ' +
30+
'be of type number. Received type string'
3131
}
3232
);
3333

0 commit comments

Comments
 (0)