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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
acc3c770e7
commit
b38c81cb44
@ -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;
|
||||
|
||||
|
@ -755,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')
|
||||
|
@ -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) {
|
||||
|
@ -461,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;
|
||||
|
@ -13,6 +13,7 @@ const {
|
||||
ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED,
|
||||
ERR_HTTP2_STATUS_INVALID,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
ERR_INVALID_ARG_VALUE,
|
||||
ERR_INVALID_CALLBACK,
|
||||
ERR_INVALID_HTTP_TOKEN
|
||||
} = require('internal/errors').codes;
|
||||
@ -312,8 +313,10 @@ class Http2ServerRequest extends Readable {
|
||||
}
|
||||
|
||||
set method(method) {
|
||||
if (typeof method !== 'string' || method.trim() === '')
|
||||
throw new ERR_INVALID_ARG_TYPE('method', 'string');
|
||||
if (typeof method !== 'string')
|
||||
throw new ERR_INVALID_ARG_TYPE('method', 'string', method);
|
||||
if (method.trim() === '')
|
||||
throw new ERR_INVALID_ARG_VALUE('method', method);
|
||||
|
||||
this[kHeaders][HTTP2_HEADER_METHOD] = method;
|
||||
}
|
||||
|
@ -54,6 +54,24 @@ assert.strictEqual(flatLongLen.toString(), check);
|
||||
});
|
||||
});
|
||||
|
||||
[[42], ['hello', Buffer.from('world')]].forEach((value) => {
|
||||
assert.throws(() => {
|
||||
Buffer.concat(value);
|
||||
}, {
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
message: 'The "list[0]" argument must be one of type Array, Buffer, ' +
|
||||
`or Uint8Array. Received type ${typeof value[0]}`
|
||||
});
|
||||
});
|
||||
|
||||
assert.throws(() => {
|
||||
Buffer.concat([Buffer.from('hello'), 3]);
|
||||
}, {
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
message: 'The "list[1]" argument must be one of type Array, Buffer, ' +
|
||||
'or Uint8Array. Received type number'
|
||||
});
|
||||
|
||||
// eslint-disable-next-line node-core/crypto-check
|
||||
const random10 = common.hasCrypto ?
|
||||
require('crypto').randomBytes(10) :
|
||||
|
@ -52,8 +52,8 @@ server.listen(0, baseOptions.host, common.mustCall(function() {
|
||||
{
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
type: TypeError,
|
||||
message: 'The "Agent option" argument must be one of type ' +
|
||||
'Agent-like Object, undefined, or false'
|
||||
message: 'The "options.agent" property must be one of type Agent-like' +
|
||||
` Object, undefined, or false. Received type ${typeof agent}`
|
||||
}
|
||||
);
|
||||
});
|
||||
|
@ -45,12 +45,12 @@ server.listen(0, common.mustCall(function() {
|
||||
// change the request method
|
||||
request.method = 'POST';
|
||||
assert.strictEqual(request.method, 'POST');
|
||||
common.expectsError(
|
||||
assert.throws(
|
||||
() => request.method = ' ',
|
||||
{
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
type: TypeError,
|
||||
message: 'The "method" argument must be of type string'
|
||||
code: 'ERR_INVALID_ARG_VALUE',
|
||||
name: 'TypeError [ERR_INVALID_ARG_VALUE]',
|
||||
message: "The argument 'method' is invalid. Received ' '"
|
||||
}
|
||||
);
|
||||
assert.throws(
|
||||
@ -58,7 +58,8 @@ server.listen(0, common.mustCall(function() {
|
||||
{
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
|
||||
message: 'The "method" argument must be of type string'
|
||||
message: 'The "method" argument must be of type string. ' +
|
||||
'Received type boolean'
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -26,8 +26,8 @@ common.expectsError(() => tls.createServer({ handshakeTimeout: 'abcd' }),
|
||||
{
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
type: TypeError,
|
||||
message: 'The "timeout" argument must ' +
|
||||
'be of type number'
|
||||
message: 'The "options.handshakeTimeout" property must ' +
|
||||
'be of type number. Received type string'
|
||||
}
|
||||
);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user