From c66e6471e780e752f3f6f9337ac49a3d6e3d7091 Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Sat, 8 Aug 2020 19:01:59 +0300 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/31251 Refs: https://github.com/nodejs/node/pull/34070#discussion_r467251009 Signed-off-by: Denys Otrishko PR-URL: https://github.com/nodejs/node/pull/34682 Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Rich Trott --- doc/api/buffer.md | 21 +++- doc/api/errors.md | 28 +++-- lib/_http_agent.js | 2 +- lib/_tls_common.js | 10 +- lib/buffer.js | 3 +- lib/dns.js | 8 +- lib/internal/child_process.js | 10 +- lib/internal/crypto/cipher.js | 4 +- lib/internal/crypto/diffiehellman.js | 6 +- lib/internal/crypto/keygen.js | 68 +++++----- lib/internal/crypto/keys.js | 14 +-- lib/internal/crypto/sig.js | 6 +- lib/internal/dns/promises.js | 6 +- lib/internal/dns/utils.js | 4 +- lib/internal/errors.js | 12 +- lib/internal/fs/utils.js | 7 +- lib/internal/http2/core.js | 26 ++-- lib/internal/modules/cjs/loader.js | 3 +- lib/internal/process/per_thread.js | 6 +- lib/internal/streams/state.js | 6 +- lib/internal/validators.js | 12 +- lib/net.js | 3 +- lib/perf_hooks.js | 6 +- lib/readline.js | 10 +- test/es-module/test-esm-dns-promises.mjs | 4 +- test/fixtures/require-resolve.js | 2 +- test/internet/test-dns-promises-resolve.js | 4 +- .../test-buffer-no-negative-allocation.js | 4 +- test/parallel/test-buffer-over-max-length.js | 4 +- test/parallel/test-buffer-slow.js | 4 +- .../test-buffer-tostring-rangeerror.js | 4 +- test/parallel/test-c-ares.js | 6 +- ...st-child-process-advanced-serialization.js | 9 +- ...child-process-fork-stdio-string-variant.js | 2 +- .../test-child-process-validate-stdio.js | 2 +- test/parallel/test-crypto-authenticated.js | 31 ++--- test/parallel/test-crypto-dh-stateless.js | 8 +- test/parallel/test-crypto-key-objects.js | 8 +- test/parallel/test-crypto-keygen.js | 116 +++++++++--------- test/parallel/test-crypto-sign-verify.js | 14 +-- test/parallel/test-dns-lookup.js | 9 +- test/parallel/test-dns.js | 8 +- .../parallel/test-fs-assert-encoding-error.js | 2 +- test/parallel/test-fs-open-flags.js | 6 +- .../test-fs-read-file-assert-encoding.js | 2 +- test/parallel/test-http-agent-scheduling.js | 6 +- ...est-http2-client-request-options-errors.js | 6 +- .../test-http2-respond-file-errors.js | 6 +- .../test-http2-respond-file-fd-errors.js | 6 +- test/parallel/test-internal-fs.js | 2 +- .../test-internal-validators-validateoneof.js | 16 --- .../parallel/test-net-connect-options-port.js | 4 +- .../test-net-server-listen-options.js | 4 +- test/parallel/test-performanceobserver.js | 6 +- test/parallel/test-process-cpuUsage.js | 12 +- ...st-readline-interface-escapecodetimeout.js | 2 +- test/parallel/test-readline-interface.js | 12 +- ...st-stream-transform-split-highwatermark.js | 10 +- test/parallel/test-streams-highwatermark.js | 8 +- test/parallel/test-tls-set-ciphers.js | 4 +- test/parallel/test-vm-module-basic.js | 2 +- .../test-worker-unsupported-eval-on-url.mjs | 2 +- .../test-performance-eventloopdelay.js | 2 +- 63 files changed, 315 insertions(+), 325 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 4902106a595de9..ad5b9b762e9909 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -288,6 +288,10 @@ It can be constructed in a variety of ways. * `size` {integer} The desired length of the new `Buffer`. Allocates a new `Buffer` of `size` bytes. If `size` is larger than -[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_OPT_VALUE`][] +[`buffer.constants.MAX_LENGTH`][] or smaller than 0, [`ERR_INVALID_ARG_VALUE`][] is thrown. A zero-length `Buffer` is created if `size` is 0. The underlying memory for `Buffer` instances created in this way is *not @@ -3274,7 +3287,7 @@ introducing security vulnerabilities into an application. [`Buffer.poolSize`]: #buffer_class_property_buffer_poolsize [`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView [`ERR_INVALID_BUFFER_SIZE`]: errors.html#ERR_INVALID_BUFFER_SIZE -[`ERR_INVALID_OPT_VALUE`]: errors.html#ERR_INVALID_OPT_VALUE +[`ERR_INVALID_ARG_VALUE`]: errors.html#ERR_INVALID_ARG_VALUE [`ERR_OUT_OF_RANGE`]: errors.html#ERR_OUT_OF_RANGE [`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify [`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer diff --git a/doc/api/errors.md b/doc/api/errors.md index e5b8a20e35a046..3a1b9468724a3f 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1378,16 +1378,6 @@ An IP address is not valid. The imported module string is an invalid URL, package name, or package subpath specifier. - -### `ERR_INVALID_OPT_VALUE` - -An invalid or unexpected value was passed in an options object. - - -### `ERR_INVALID_OPT_VALUE_ENCODING` - -An invalid or unknown file encoding was passed. - ### `ERR_INVALID_PACKAGE_CONFIG` @@ -2391,6 +2381,24 @@ Used when an invalid character is found in an HTTP response status message --> A given index was out of the accepted range (e.g. negative offsets). + +### `ERR_INVALID_OPT_VALUE` + + +An invalid or unexpected value was passed in an options object. + + +### `ERR_INVALID_OPT_VALUE_ENCODING` + + +An invalid or unknown file encoding was passed. + ### `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`