Skip to content

Commit

Permalink
lib: improve error handling
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
BridgeAR committed Mar 25, 2018
1 parent acc3c77 commit b38c81c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 16 deletions.
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
2 changes: 1 addition & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
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
5 changes: 4 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-buffer-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) :
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-http-client-reject-unexpected-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
);
});
Expand Down
11 changes: 6 additions & 5 deletions test/parallel/test-http2-compat-serverrequest-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ 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(
() => request.method = true,
{
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'
}
);

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-tls-basic-validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
);

Expand Down

0 comments on commit b38c81c

Please sign in to comment.