Skip to content

Commit 76d9760

Browse files
committed
lib: refactor to use validators in http2
Refs: nodejs#46101
1 parent 384e1b5 commit 76d9760

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

lib/internal/http2/core.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ const {
126126
validateNumber,
127127
validateString,
128128
validateUint32,
129-
validateAbortSignal
129+
validateAbortSignal,
130+
validateBoolean,
130131
} = require('internal/validators');
131132
const fsPromisesInternal = require('internal/fs/promises');
132133
const { utcDate } = require('internal/http');
@@ -761,27 +762,27 @@ function requestOnConnect(headers, options) {
761762
const setAndValidatePriorityOptions = hideStackFrames((options) => {
762763
if (options.weight === undefined) {
763764
options.weight = NGHTTP2_DEFAULT_WEIGHT;
764-
} else if (typeof options.weight !== 'number') {
765-
throw new ERR_INVALID_ARG_VALUE('options.weight', options.weight);
766-
}
765+
}
766+
767+
validateNumber(options.weight, 'options.weight');
767768

768769
if (options.parent === undefined) {
769770
options.parent = 0;
770-
} else if (typeof options.parent !== 'number' || options.parent < 0) {
771-
throw new ERR_INVALID_ARG_VALUE('options.parent', options.parent);
772-
}
771+
}
772+
773+
validateNumber(options.parent, 'options.parent', 0);
773774

774775
if (options.exclusive === undefined) {
775776
options.exclusive = false;
776-
} else if (typeof options.exclusive !== 'boolean') {
777-
throw new ERR_INVALID_ARG_VALUE('options.exclusive', options.exclusive);
778777
}
779778

779+
validateBoolean(options.exclusive, 'options.exclusive');
780+
780781
if (options.silent === undefined) {
781782
options.silent = false;
782-
} else if (typeof options.silent !== 'boolean') {
783-
throw new ERR_INVALID_ARG_VALUE('options.silent', options.silent);
784783
}
784+
785+
validateBoolean(options.silent, 'options.silent');
785786
});
786787

787788
// When an error occurs internally at the binding level, immediately
@@ -1784,10 +1785,11 @@ class ClientHttp2Session extends Http2Session {
17841785
// stream by default if the user has not specifically indicated a
17851786
// preference.
17861787
options.endStream = isPayloadMeaningless(headers[HTTP2_HEADER_METHOD]);
1787-
} else if (typeof options.endStream !== 'boolean') {
1788-
throw new ERR_INVALID_ARG_VALUE('options.endStream', options.endStream);
17891788
}
17901789

1790+
validateBoolean(options.endStream, 'options.endStream');
1791+
1792+
17911793
const headersList = mapToHeaders(headers);
17921794

17931795
const stream = new ClientHttp2Stream(this, undefined, undefined, {});

test/parallel/test-http2-client-request-options-errors.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ const types = {
2828
symbol: Symbol('test')
2929
};
3030

31+
function determineSpecificType(value) {
32+
if (value == null) {
33+
return '' + value;
34+
}
35+
if (typeof value === 'function' && value.name) {
36+
return `function ${value.name}`;
37+
}
38+
if (typeof value === 'object') {
39+
if (value.constructor?.name) {
40+
return `an instance of ${value.constructor.name}`;
41+
}
42+
return `${inspect(value, { depth: -1 })}`;
43+
}
44+
let inspected = inspect(value, { colors: false });
45+
if (inspected.length > 28) { inspected = `${StringPrototypeSlice(inspected, 0, 25)}...`; }
46+
47+
return `type ${typeof value} (${inspected})`;
48+
}
49+
3150
const server = http2.createServer(common.mustNotCall());
3251

3352
server.listen(0, common.mustCall(() => {
@@ -48,9 +67,9 @@ server.listen(0, common.mustCall(() => {
4867
[option]: types[type]
4968
}), {
5069
name: 'TypeError',
51-
code: 'ERR_INVALID_ARG_VALUE',
52-
message: `The property 'options.${option}' is invalid. ` +
53-
`Received ${inspect(types[type])}`
70+
code: 'ERR_INVALID_ARG_TYPE',
71+
message: `The "options.${option}" property must be of type ${optionsToTest[option]}. ` +
72+
`Received ${determineSpecificType(types[type])}`
5473
});
5574
});
5675
});

0 commit comments

Comments
 (0)