Skip to content

Commit c66e647

Browse files
committed
lib: remove ERR_INVALID_OPT_VALUE and ERR_INVALID_OPT_VALUE_ENCODING
This will be a start to generalize all argument validation errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more specific errors. The OPT errors didn't bring much to the errors as it's just another variant of ARG error which is sometimes more confusing (some of our code used OPT errors to denote just argument validation errors presumably because of similarity of OPT to 'option' and not 'options-object') and they don't specify the name of the options object where the invalid value is located. Much better approach would be to just specify path to the invalid value in the name of the value as it is done in this PR (i.e. 'options.format', 'options.publicKey.type' etc) Also since this decreases a variety of errors we have it'd be easier to reuse validation code across the codebase. Refs: #31251 Refs: #34070 (comment) Signed-off-by: Denys Otrishko <[email protected]> PR-URL: #34682 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 8a8ca4b commit c66e647

File tree

63 files changed

+315
-325
lines changed

Some content is hidden

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

63 files changed

+315
-325
lines changed

doc/api/buffer.md

+17-4
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ It can be constructed in a variety of ways.
288288
<!-- YAML
289289
added: v5.10.0
290290
changes:
291+
- version: REPLACEME
292+
pr-url: https://github.com/nodejs/node/pull/34682
293+
description: Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE
294+
for invalid input arguments.
291295
- version: v10.0.0
292296
pr-url: https://github.com/nodejs/node/pull/18129
293297
description: Attempting to fill a non-zero length buffer with a zero length
@@ -319,7 +323,7 @@ console.log(buf);
319323
```
320324

321325
If `size` is larger than
322-
[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_OPT_VALUE`][]
326+
[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_ARG_VALUE`][]
323327
is thrown.
324328

325329
If `fill` is specified, the allocated `Buffer` will be initialized by calling
@@ -353,6 +357,10 @@ A `TypeError` will be thrown if `size` is not a number.
353357
<!-- YAML
354358
added: v5.10.0
355359
changes:
360+
- version: REPLACEME
361+
pr-url: https://github.com/nodejs/node/pull/34682
362+
description: Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE
363+
for invalid input arguments.
356364
- version: v7.0.0
357365
pr-url: https://github.com/nodejs/node/pull/7079
358366
description: Passing a negative `size` will now throw an error.
@@ -361,7 +369,7 @@ changes:
361369
* `size` {integer} The desired length of the new `Buffer`.
362370

363371
Allocates a new `Buffer` of `size` bytes. If `size` is larger than
364-
[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_OPT_VALUE`][]
372+
[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_ARG_VALUE`][]
365373
is thrown.
366374

367375
The underlying memory for `Buffer` instances created in this way is *not
@@ -401,12 +409,17 @@ additional performance that [`Buffer.allocUnsafe()`][] provides.
401409
### Static method: `Buffer.allocUnsafeSlow(size)`
402410
<!-- YAML
403411
added: v5.12.0
412+
changes:
413+
- version: REPLACEME
414+
pr-url: https://github.com/nodejs/node/pull/34682
415+
description: Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE
416+
for invalid input arguments.
404417
-->
405418

406419
* `size` {integer} The desired length of the new `Buffer`.
407420

408421
Allocates a new `Buffer` of `size` bytes. If `size` is larger than
409-
[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_OPT_VALUE`][]
422+
[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_ARG_VALUE`][]
410423
is thrown. A zero-length `Buffer` is created if `size` is 0.
411424

412425
The underlying memory for `Buffer` instances created in this way is *not
@@ -3274,7 +3287,7 @@ introducing security vulnerabilities into an application.
32743287
[`Buffer.poolSize`]: #buffer_class_property_buffer_poolsize
32753288
[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
32763289
[`ERR_INVALID_BUFFER_SIZE`]: errors.html#ERR_INVALID_BUFFER_SIZE
3277-
[`ERR_INVALID_OPT_VALUE`]: errors.html#ERR_INVALID_OPT_VALUE
3290+
[`ERR_INVALID_ARG_VALUE`]: errors.html#ERR_INVALID_ARG_VALUE
32783291
[`ERR_OUT_OF_RANGE`]: errors.html#ERR_OUT_OF_RANGE
32793292
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
32803293
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer

doc/api/errors.md

+18-10
Original file line numberDiff line numberDiff line change
@@ -1378,16 +1378,6 @@ An IP address is not valid.
13781378
The imported module string is an invalid URL, package name, or package subpath
13791379
specifier.
13801380

1381-
<a id="ERR_INVALID_OPT_VALUE"></a>
1382-
### `ERR_INVALID_OPT_VALUE`
1383-
1384-
An invalid or unexpected value was passed in an options object.
1385-
1386-
<a id="ERR_INVALID_OPT_VALUE_ENCODING"></a>
1387-
### `ERR_INVALID_OPT_VALUE_ENCODING`
1388-
1389-
An invalid or unknown file encoding was passed.
1390-
13911381
<a id="ERR_INVALID_PACKAGE_CONFIG"></a>
13921382
### `ERR_INVALID_PACKAGE_CONFIG`
13931383

@@ -2391,6 +2381,24 @@ Used when an invalid character is found in an HTTP response status message
23912381
-->
23922382
A given index was out of the accepted range (e.g. negative offsets).
23932383

2384+
<a id="ERR_INVALID_OPT_VALUE"></a>
2385+
### `ERR_INVALID_OPT_VALUE`
2386+
<!-- YAML
2387+
added: v8.0.0
2388+
removed: REPLACEME
2389+
-->
2390+
2391+
An invalid or unexpected value was passed in an options object.
2392+
2393+
<a id="ERR_INVALID_OPT_VALUE_ENCODING"></a>
2394+
### `ERR_INVALID_OPT_VALUE_ENCODING`
2395+
<!-- YAML
2396+
added: v9.0.0
2397+
removed: REPLACEME
2398+
-->
2399+
2400+
An invalid or unknown file encoding was passed.
2401+
23942402
<a id="ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST"></a>
23952403
### `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`
23962404
<!-- YAML

lib/_http_agent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function Agent(options) {
9898
this.maxTotalSockets = this.options.maxTotalSockets;
9999
this.totalSocketCount = 0;
100100

101-
validateOneOf(this.scheduling, 'scheduling', ['fifo', 'lifo'], true);
101+
validateOneOf(this.scheduling, 'scheduling', ['fifo', 'lifo']);
102102

103103
if (this.maxTotalSockets !== undefined) {
104104
validateNumber(this.maxTotalSockets, 'maxTotalSockets');

lib/_tls_common.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const tls = require('tls');
3232
const {
3333
ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED,
3434
ERR_INVALID_ARG_TYPE,
35-
ERR_INVALID_OPT_VALUE,
35+
ERR_INVALID_ARG_VALUE,
3636
ERR_TLS_INVALID_PROTOCOL_VERSION,
3737
ERR_TLS_PROTOCOL_VERSION_CONFLICT,
3838
} = require('internal/errors').codes;
@@ -157,7 +157,7 @@ exports.createSecureContext = function createSecureContext(options) {
157157
}
158158

159159
if (sigalgs === '') {
160-
throw new ERR_INVALID_OPT_VALUE('sigalgs', sigalgs);
160+
throw new ERR_INVALID_ARG_VALUE('options.sigalgs', sigalgs);
161161
}
162162

163163
c.context.setSigalgs(sigalgs);
@@ -167,12 +167,12 @@ exports.createSecureContext = function createSecureContext(options) {
167167
if (privateKeyIdentifier !== undefined) {
168168
if (privateKeyEngine === undefined) {
169169
// Engine is required when privateKeyIdentifier is present
170-
throw new ERR_INVALID_OPT_VALUE('privateKeyEngine',
170+
throw new ERR_INVALID_ARG_VALUE('options.privateKeyEngine',
171171
privateKeyEngine);
172172
}
173173
if (key) {
174174
// Both data key and engine key can't be set at the same time
175-
throw new ERR_INVALID_OPT_VALUE('privateKeyIdentifier',
175+
throw new ERR_INVALID_ARG_VALUE('options.privateKeyIdentifier',
176176
privateKeyIdentifier);
177177
}
178178

@@ -210,7 +210,7 @@ exports.createSecureContext = function createSecureContext(options) {
210210
if (cipherSuites === '' && cipherList === '') {
211211
// Specifying empty cipher suites for both TLS1.2 and TLS1.3 is invalid, its
212212
// not possible to handshake with no suites.
213-
throw new ERR_INVALID_OPT_VALUE('ciphers', ciphers);
213+
throw new ERR_INVALID_ARG_VALUE('options.ciphers', ciphers);
214214
}
215215

216216
c.context.setCipherSuites(cipherSuites);

lib/buffer.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ const {
8686
ERR_INVALID_ARG_TYPE,
8787
ERR_INVALID_ARG_VALUE,
8888
ERR_INVALID_BUFFER_SIZE,
89-
ERR_INVALID_OPT_VALUE,
9089
ERR_OUT_OF_RANGE,
9190
ERR_UNKNOWN_ENCODING
9291
},
@@ -342,7 +341,7 @@ const assertSize = hideStackFrames((size) => {
342341
throw new ERR_INVALID_ARG_TYPE('size', 'number', size);
343342
}
344343
if (!(size >= 0 && size <= kMaxLength)) {
345-
throw new ERR_INVALID_OPT_VALUE.RangeError('size', size);
344+
throw new ERR_INVALID_ARG_VALUE.RangeError('size', size);
346345
}
347346
});
348347

lib/dns.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ const {
4242
} = require('internal/dns/utils');
4343
const {
4444
ERR_INVALID_ARG_TYPE,
45+
ERR_INVALID_ARG_VALUE,
4546
ERR_INVALID_CALLBACK,
46-
ERR_INVALID_OPT_VALUE,
4747
ERR_MISSING_ARGS,
4848
} = errors.codes;
4949
const {
@@ -115,7 +115,7 @@ function lookup(hostname, options, callback) {
115115
family = options >>> 0;
116116
}
117117

118-
validateOneOf(family, 'family', [0, 4, 6], true);
118+
validateOneOf(family, 'family', [0, 4, 6]);
119119

120120
if (!hostname) {
121121
emitInvalidHostnameWarning(hostname);
@@ -171,7 +171,7 @@ function lookupService(address, port, callback) {
171171
throw new ERR_MISSING_ARGS('address', 'port', 'callback');
172172

173173
if (isIP(address) === 0)
174-
throw new ERR_INVALID_OPT_VALUE('address', address);
174+
throw new ERR_INVALID_ARG_VALUE('address', address);
175175

176176
validatePort(port);
177177

@@ -262,7 +262,7 @@ function resolve(hostname, rrtype, callback) {
262262
if (typeof resolver === 'function') {
263263
return resolver.call(this, hostname, callback);
264264
}
265-
throw new ERR_INVALID_OPT_VALUE('rrtype', rrtype);
265+
throw new ERR_INVALID_ARG_VALUE('rrtype', rrtype);
266266
}
267267

268268
function defaultResolverSetServers(servers) {

lib/internal/child_process.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const {
1111
errnoException,
1212
codes: {
1313
ERR_INVALID_ARG_TYPE,
14+
ERR_INVALID_ARG_VALUE,
1415
ERR_INVALID_HANDLE_TYPE,
15-
ERR_INVALID_OPT_VALUE,
1616
ERR_INVALID_SYNC_FORK_INPUT,
1717
ERR_IPC_CHANNEL_CLOSED,
1818
ERR_IPC_DISCONNECTED,
@@ -226,7 +226,7 @@ function stdioStringToArray(stdio, channel) {
226226
case 'pipe': options.push(stdio, stdio, stdio); break;
227227
case 'inherit': options.push(0, 1, 2); break;
228228
default:
229-
throw new ERR_INVALID_OPT_VALUE('stdio', stdio);
229+
throw new ERR_INVALID_ARG_VALUE('stdio', stdio);
230230
}
231231

232232
if (channel) options.push(channel);
@@ -347,7 +347,7 @@ ChildProcess.prototype.spawn = function(options) {
347347

348348

349349
validateOneOf(options.serialization, 'options.serialization',
350-
[undefined, 'json', 'advanced'], true);
350+
[undefined, 'json', 'advanced']);
351351
const serialization = options.serialization || 'json';
352352

353353
if (ipc !== undefined) {
@@ -934,7 +934,7 @@ function getValidStdio(stdio, sync) {
934934
if (typeof stdio === 'string') {
935935
stdio = stdioStringToArray(stdio);
936936
} else if (!ArrayIsArray(stdio)) {
937-
throw new ERR_INVALID_OPT_VALUE('stdio', stdio);
937+
throw new ERR_INVALID_ARG_VALUE('stdio', stdio);
938938
}
939939

940940
// At least 3 stdio will be created
@@ -1019,7 +1019,7 @@ function getValidStdio(stdio, sync) {
10191019
} else {
10201020
// Cleanup
10211021
cleanup();
1022-
throw new ERR_INVALID_OPT_VALUE('stdio', stdio);
1022+
throw new ERR_INVALID_ARG_VALUE('stdio', stdio);
10231023
}
10241024

10251025
return acc;

lib/internal/crypto/cipher.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const {
1212
const {
1313
ERR_CRYPTO_INVALID_STATE,
1414
ERR_INVALID_ARG_TYPE,
15-
ERR_INVALID_OPT_VALUE
15+
ERR_INVALID_ARG_VALUE
1616
} = require('internal/errors').codes;
1717
const { validateEncoding, validateString } = require('internal/validators');
1818

@@ -87,7 +87,7 @@ function getUIntOption(options, key) {
8787
let value;
8888
if (options && (value = options[key]) != null) {
8989
if (value >>> 0 !== value)
90-
throw new ERR_INVALID_OPT_VALUE(key, value);
90+
throw new ERR_INVALID_ARG_VALUE(`options.${key}`, value);
9191
return value;
9292
}
9393
return -1;

lib/internal/crypto/diffiehellman.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const {
1212
ERR_CRYPTO_INCOMPATIBLE_KEY,
1313
ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE,
1414
ERR_INVALID_ARG_TYPE,
15-
ERR_INVALID_OPT_VALUE
15+
ERR_INVALID_ARG_VALUE,
1616
} = require('internal/errors').codes;
1717
const {
1818
validateString,
@@ -258,10 +258,10 @@ function diffieHellman(options) {
258258

259259
const { privateKey, publicKey } = options;
260260
if (!(privateKey instanceof KeyObject))
261-
throw new ERR_INVALID_OPT_VALUE('privateKey', privateKey);
261+
throw new ERR_INVALID_ARG_VALUE('options.privateKey', privateKey);
262262

263263
if (!(publicKey instanceof KeyObject))
264-
throw new ERR_INVALID_OPT_VALUE('publicKey', publicKey);
264+
throw new ERR_INVALID_ARG_VALUE('options.publicKey', publicKey);
265265

266266
if (privateKey.type !== 'private')
267267
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(privateKey.type, 'private');

0 commit comments

Comments
 (0)