From 4431dc8f63233f27576549d1097cc48fca62306c Mon Sep 17 00:00:00 2001 From: Ian Bull Date: Wed, 28 Aug 2024 16:40:37 -0400 Subject: [PATCH] refactor(ext): throw new error instead of throw error (#25272) To ensure consistency across the codebase, this commit refactors the code in the `ext` folder to use `throw new Error`` instead of `throw` for throwing errors. Fixes https://github.com/denoland/deno/issues/25270 --- ext/fetch/26_fetch.js | 2 +- ext/http/00_serve.ts | 8 ++++---- ext/node/polyfills/01_require.js | 2 +- ext/node/polyfills/02_init.js | 2 +- ext/node/polyfills/_process/process.ts | 2 +- ext/node/polyfills/internal/util/comparisons.ts | 2 +- ext/node/polyfills/os.ts | 2 +- ext/web/00_infra.js | 2 +- ext/web/06_streams.js | 6 +++--- ext/webidl/00_webidl.js | 2 +- 10 files changed, 15 insertions(+), 15 deletions(-) diff --git a/ext/fetch/26_fetch.js b/ext/fetch/26_fetch.js index e2809187b9..47186fb2dd 100644 --- a/ext/fetch/26_fetch.js +++ b/ext/fetch/26_fetch.js @@ -145,7 +145,7 @@ async function mainFetch(req, recursive, terminator) { reqRid = resourceForReadableStream(stream, req.body.length); } } else { - throw TypeError("invalid body"); + throw new TypeError("invalid body"); } } diff --git a/ext/http/00_serve.ts b/ext/http/00_serve.ts index 02910d580d..c8f4e0604b 100644 --- a/ext/http/00_serve.ts +++ b/ext/http/00_serve.ts @@ -457,7 +457,7 @@ function fastSyncResponseOrStream( // At this point in the response it needs to be a stream if (!ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, stream)) { innerRequest?.close(); - throw TypeError("invalid response"); + throw new TypeError("invalid response"); } const resourceBacking = getReadableStreamResourceBacking(stream); let rid, autoClose; @@ -506,13 +506,13 @@ function mapToCallback(context, callback, onError) { // Throwing Error if the handler return value is not a Response class if (!ObjectPrototypeIsPrototypeOf(ResponsePrototype, response)) { - throw TypeError( + throw new TypeError( "Return value from serve handler must be a response or a promise resolving to a response", ); } if (response.bodyUsed) { - throw TypeError( + throw new TypeError( "The body of the Response returned from the serve handler has already been consumed.", ); } @@ -520,7 +520,7 @@ function mapToCallback(context, callback, onError) { try { response = await onError(error); if (!ObjectPrototypeIsPrototypeOf(ResponsePrototype, response)) { - throw TypeError( + throw new TypeError( "Return value from onError handler must be a response or a promise resolving to a response", ); } diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index aec8c55b8d..d666b39274 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -890,7 +890,7 @@ Module._preloadModules = function (requests) { Module.prototype.load = function (filename) { if (this.loaded) { - throw Error("Module already loaded"); + throw new Error("Module already loaded"); } // Canonicalize the path so it's not pointing to the symlinked directory diff --git a/ext/node/polyfills/02_init.js b/ext/node/polyfills/02_init.js index 870ff12f35..8a4aa8e972 100644 --- a/ext/node/polyfills/02_init.js +++ b/ext/node/polyfills/02_init.js @@ -22,7 +22,7 @@ function initialize(args) { } = args; if (!warmup) { if (initialized) { - throw Error("Node runtime already initialized"); + throw new Error("Node runtime already initialized"); } initialized = true; if (usesLocalNodeModulesDir) { diff --git a/ext/node/polyfills/_process/process.ts b/ext/node/polyfills/_process/process.ts index 5abde1cf0e..e4b88a11af 100644 --- a/ext/node/polyfills/_process/process.ts +++ b/ext/node/polyfills/_process/process.ts @@ -32,7 +32,7 @@ export function arch(): string { } else if (build.arch == "riscv64gc") { return "riscv64"; } else { - throw Error("unreachable"); + throw new Error("unreachable"); } } diff --git a/ext/node/polyfills/internal/util/comparisons.ts b/ext/node/polyfills/internal/util/comparisons.ts index 65350a8136..39e30c69aa 100644 --- a/ext/node/polyfills/internal/util/comparisons.ts +++ b/ext/node/polyfills/internal/util/comparisons.ts @@ -432,7 +432,7 @@ function isEqualBoxedPrimitive(a: any, b: any): boolean { } // assert.fail(`Unknown boxed type ${val1}`); // return false; - throw Error(`Unknown boxed type`); + throw new Error(`Unknown boxed type`); } function getEnumerables(val: any, keys: any) { diff --git a/ext/node/polyfills/os.ts b/ext/node/polyfills/os.ts index 1cd466ec21..e47e8679ec 100644 --- a/ext/node/polyfills/os.ts +++ b/ext/node/polyfills/os.ts @@ -311,7 +311,7 @@ export function type(): string { case "openbsd": return "OpenBSD"; default: - throw Error("unreachable"); + throw new Error("unreachable"); } } diff --git a/ext/web/00_infra.js b/ext/web/00_infra.js index e42f2cc931..4b241ab5d7 100644 --- a/ext/web/00_infra.js +++ b/ext/web/00_infra.js @@ -133,7 +133,7 @@ function regexMatcher(chars) { ); return `\\u${a}-\\u${b}`; } else { - throw TypeError("unreachable"); + throw new TypeError("unreachable"); } }); return ArrayPrototypeJoin(matchers, ""); diff --git a/ext/web/06_streams.js b/ext/web/06_streams.js index 130b24783e..3da3fe36cb 100644 --- a/ext/web/06_streams.js +++ b/ext/web/06_streams.js @@ -524,10 +524,10 @@ function dequeueValue(container) { function enqueueValueWithSize(container, value, size) { assert(container[_queue] && typeof container[_queueTotalSize] === "number"); if (isNonNegativeNumber(size) === false) { - throw RangeError("chunk size isn't a positive number"); + throw new RangeError("chunk size isn't a positive number"); } if (size === Infinity) { - throw RangeError("chunk size is invalid"); + throw new RangeError("chunk size is invalid"); } container[_queue].enqueue({ value, size }); container[_queueTotalSize] += size; @@ -543,7 +543,7 @@ function extractHighWaterMark(strategy, defaultHWM) { } const highWaterMark = strategy.highWaterMark; if (NumberIsNaN(highWaterMark) || highWaterMark < 0) { - throw RangeError( + throw new RangeError( `Expected highWaterMark to be a positive number or Infinity, got "${highWaterMark}".`, ); } diff --git a/ext/webidl/00_webidl.js b/ext/webidl/00_webidl.js index 9ea2200f33..36fc89ce29 100644 --- a/ext/webidl/00_webidl.js +++ b/ext/webidl/00_webidl.js @@ -95,7 +95,7 @@ function makeException(ErrorType, message, prefix, context) { function toNumber(value) { if (typeof value === "bigint") { - throw TypeError("Cannot convert a BigInt value to a number"); + throw new TypeError("Cannot convert a BigInt value to a number"); } return Number(value); }