From ace459598a7f4edef96e129faeb600778fee3d11 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 6 Jan 2025 23:51:46 -0800 Subject: [PATCH] update $ERR_INVALID_ARG_VALUE callsites (#16202) --- src/bun.js/bindings/ErrorCode.cpp | 7 ------- src/js/bun/sql.ts | 20 ++++++++++++++++---- src/js/internal/validators.ts | 10 +++++++--- src/js/node/http2.ts | 13 ++++++------- src/js/node/stream.ts | 2 +- src/js/node/v8.ts | 2 +- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/bun.js/bindings/ErrorCode.cpp b/src/bun.js/bindings/ErrorCode.cpp index bbf1bd6728..1e5d8e720c 100644 --- a/src/bun.js/bindings/ErrorCode.cpp +++ b/src/bun.js/bindings/ErrorCode.cpp @@ -820,13 +820,6 @@ JSC_DEFINE_HOST_FUNCTION(Bun::jsFunctionMakeErrorWithCode, (JSC::JSGlobalObject JSValue arg0 = callFrame->argument(1); JSValue arg1 = callFrame->argument(2); JSValue arg2 = callFrame->argument(3); - - // TODO: remove this if; this switch case was added but not all the callsites using bare $ERR_INVALID_ARG_VALUE(msg) were updated - if (callFrame->argumentCount() == 2 && arg0.isString()) { - auto message = arg0.toWTFString(globalObject); - RETURN_IF_EXCEPTION(scope, {}); - return JSC::JSValue::encode(createError(globalObject, error, message)); - } return JSValue::encode(ERR_INVALID_ARG_TYPE(scope, globalObject, arg0, arg1, arg2)); } diff --git a/src/js/bun/sql.ts b/src/js/bun/sql.ts index 8ce069cb72..abe2a973cc 100644 --- a/src/js/bun/sql.ts +++ b/src/js/bun/sql.ts @@ -65,7 +65,7 @@ function normalizeSSLMode(value: string): SSLMode { } } - throw $ERR_INVALID_ARG_VALUE(`Invalid SSL mode: ${value}`); + throw $ERR_INVALID_ARG_VALUE("sslmode", value); } class Query extends PublicPromise { @@ -454,21 +454,33 @@ function loadOptions(o) { if (idleTimeout != null) { idleTimeout = Number(idleTimeout); if (idleTimeout > 2 ** 31 || idleTimeout < 0 || idleTimeout !== idleTimeout) { - throw $ERR_INVALID_ARG_VALUE("idle_timeout must be a non-negative integer less than 2^31"); + throw $ERR_INVALID_ARG_VALUE( + "options.idle_timeout", + idleTimeout, + "must be a non-negative integer less than 2^31", + ); } } if (connectionTimeout != null) { connectionTimeout = Number(connectionTimeout); if (connectionTimeout > 2 ** 31 || connectionTimeout < 0 || connectionTimeout !== connectionTimeout) { - throw $ERR_INVALID_ARG_VALUE("connection_timeout must be a non-negative integer less than 2^31"); + throw $ERR_INVALID_ARG_VALUE( + "options.connection_timeout", + connectionTimeout, + "must be a non-negative integer less than 2^31", + ); } } if (maxLifetime != null) { maxLifetime = Number(maxLifetime); if (maxLifetime > 2 ** 31 || maxLifetime < 0 || maxLifetime !== maxLifetime) { - throw $ERR_INVALID_ARG_VALUE("max_lifetime must be a non-negative integer less than 2^31"); + throw $ERR_INVALID_ARG_VALUE( + "options.max_lifetime", + maxLifetime, + "must be a non-negative integer less than 2^31", + ); } } diff --git a/src/js/internal/validators.ts b/src/js/internal/validators.ts index 414b943e7f..2df37ba6ea 100644 --- a/src/js/internal/validators.ts +++ b/src/js/internal/validators.ts @@ -1,10 +1,10 @@ const { hideFromStack } = require("internal/shared"); -const { ArrayIsArray } = require("internal/primordials"); const RegExpPrototypeExec = RegExp.prototype.exec; const ArrayPrototypeIncludes = Array.prototype.includes; const ArrayPrototypeJoin = Array.prototype.join; const ArrayPrototypeMap = Array.prototype.map; +const ArrayIsArray = Array.isArray; const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/; /** @@ -28,7 +28,9 @@ const linkValueRegExp = /^(?:<[^>]*>)(?:\s*;\s*[^;"\s]+(?:=(")?[^;"\s]*\1)?)*$/; function validateLinkHeaderFormat(value, name) { if (typeof value === "undefined" || !RegExpPrototypeExec.$call(linkValueRegExp, value)) { throw $ERR_INVALID_ARG_VALUE( - `The arguments ${name} is invalid must be an array or string of format "; rel=preload; as=style"`, + name, + value, + `must be an array or string of format "; rel=preload; as=style"`, ); } } @@ -59,7 +61,9 @@ function validateLinkHeaderValue(hints) { } throw $ERR_INVALID_ARG_VALUE( - `The arguments hints is invalid must be an array or string of format "; rel=preload; as=style"`, + "hints", + hints, + `must be an array or string of format "; rel=preload; as=style"`, ); } hideFromStack(validateLinkHeaderValue); diff --git a/src/js/node/http2.ts b/src/js/node/http2.ts index bb7544bdbc..a48c0027bc 100644 --- a/src/js/node/http2.ts +++ b/src/js/node/http2.ts @@ -349,8 +349,7 @@ class Http2ServerRequest extends Readable { set method(method) { validateString(method, "method"); - if (StringPrototypeTrim(method) === "") - throw $ERR_INVALID_ARG_VALUE(`The arguments method is invalid. Received ${method}`); + if (StringPrototypeTrim(method) === "") throw $ERR_INVALID_ARG_VALUE("method", method); this[kHeaders][HTTP2_HEADER_METHOD] = method; } @@ -642,7 +641,7 @@ class Http2ServerResponse extends Stream { } } else { if (headers.length % 2 !== 0) { - throw $ERR_INVALID_ARG_VALUE(`The arguments headers is invalid.`); + throw $ERR_INVALID_ARG_VALUE("headers", headers); } for (i = 0; i < headers.length; i += 2) { @@ -1637,7 +1636,7 @@ class Http2Stream extends Duplex { const sensitiveNames = {}; if (sensitives) { if (!$isJSArray(sensitives)) { - throw $ERR_INVALID_ARG_VALUE("The arguments headers[http2.neverIndex] is invalid"); + throw $ERR_INVALID_ARG_VALUE("headers[http2.neverIndex]", sensitives); } for (let i = 0; i < sensitives.length; i++) { sensitiveNames[sensitives[i]] = true; @@ -2048,7 +2047,7 @@ class ServerHttp2Stream extends Http2Stream { const sensitiveNames = {}; if (sensitives) { if (!$isArray(sensitives)) { - throw $ERR_INVALID_ARG_VALUE("The arguments headers[http2.neverIndex] is invalid."); + throw $ERR_INVALID_ARG_VALUE("headers[http2.neverIndex]", sensitives); } for (let i = 0; i < sensitives.length; i++) { sensitiveNames[sensitives[i]] = true; @@ -2099,7 +2098,7 @@ class ServerHttp2Stream extends Http2Stream { const sensitiveNames = {}; if (sensitives) { if (!$isArray(sensitives)) { - throw $ERR_INVALID_ARG_VALUE("The arguments headers[http2.neverIndex] is invalid."); + throw $ERR_INVALID_ARG_VALUE("headers[http2.neverIndex]", sensitives); } for (let i = 0; i < sensitives.length; i++) { sensitiveNames[sensitives[i]] = true; @@ -3091,7 +3090,7 @@ class ClientHttp2Session extends Http2Session { const sensitiveNames = {}; if (sensitives) { if (!$isArray(sensitives)) { - throw $ERR_INVALID_ARG_VALUE("The arguments headers[http2.neverIndex] is invalid."); + throw $ERR_INVALID_ARG_VALUE("headers[http2.neverIndex]", sensitives); } for (let i = 0; i < sensitives.length; i++) { sensitiveNames[sensitives[i]] = true; diff --git a/src/js/node/stream.ts b/src/js/node/stream.ts index 2a7f2b88e7..49433352a4 100644 --- a/src/js/node/stream.ts +++ b/src/js/node/stream.ts @@ -2361,7 +2361,7 @@ var require_readable = __commonJS({ } = options; if (encoding !== undefined && !Buffer.isEncoding(encoding)) - throw $ERR_INVALID_ARG_VALUE(encoding, "options.encoding"); + throw $ERR_INVALID_ARG_VALUE("options.encoding", encoding); validateBoolean(objectMode, "options.objectMode"); // validateBoolean(native, "options.native"); diff --git a/src/js/node/v8.ts b/src/js/node/v8.ts index 5303d1b4e0..082fbfe1dc 100644 --- a/src/js/node/v8.ts +++ b/src/js/node/v8.ts @@ -133,7 +133,7 @@ function writeHeapSnapshot(path, options) { } if (!path) { - throw $ERR_INVALID_ARG_VALUE("path must be a non-empty string"); + throw $ERR_INVALID_ARG_VALUE("path", path, "must be a non-empty string"); } } else { path = getDefaultHeapSnapshotPath();