From 833a24f088a84f98c66faf93a29b9005d77acbed Mon Sep 17 00:00:00 2001 From: Ian Bull Date: Thu, 22 Aug 2024 02:02:58 -0400 Subject: [PATCH] refactor(async): align the error messages to the style guide (#5758) Co-authored-by: Yoshiya Hinosawa --- async/pool.ts | 3 ++- async/pool_test.ts | 2 +- async/retry.ts | 18 ++++++++++++++---- async/retry_test.ts | 6 +++--- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/async/pool.ts b/async/pool.ts index a86258772..e9cc353f7 100644 --- a/async/pool.ts +++ b/async/pool.ts @@ -2,7 +2,8 @@ // This module is browser compatible. /** Error message emitted from the thrown error while mapping. */ -const ERROR_WHILE_MAPPING_MESSAGE = "Threw while mapping."; +const ERROR_WHILE_MAPPING_MESSAGE = + "Cannot complete the mapping as an error was thrown from an item"; /** * pooledMap transforms values from an (async) iterable into another async diff --git a/async/pool_test.ts b/async/pool_test.ts index 99aea9bbc..2fbc5edad 100644 --- a/async/pool_test.ts +++ b/async/pool_test.ts @@ -38,7 +38,7 @@ Deno.test("pooledMap() handles errors", async () => { } }, AggregateError, - "Threw while mapping.", + "Cannot complete the mapping as an error was thrown from an item", ); assertEquals(error.errors.length, 2); assertStringIncludes(error.errors[0].stack, "Error: Bad number: 1"); diff --git a/async/retry.ts b/async/retry.ts index 076886aeb..de86e14b1 100644 --- a/async/retry.ts +++ b/async/retry.ts @@ -134,11 +134,21 @@ export async function retry( ...opts, }; - if (options.maxTimeout <= 0) throw new TypeError("maxTimeout is less than 0"); - if (options.minTimeout > options.maxTimeout) { - throw new TypeError("minTimeout is greater than maxTimeout"); + if (options.maxTimeout <= 0) { + throw new TypeError( + `Cannot retry as 'maxTimeout' must be positive: current value is ${options.maxTimeout}`, + ); + } + if (options.minTimeout > options.maxTimeout) { + throw new TypeError( + `Cannot retry as 'minTimeout' must be <= 'maxTimeout': current values 'minTimeout=${options.minTimeout}', 'maxTimeout=${options.maxTimeout}'`, + ); + } + if (options.jitter > 1) { + throw new TypeError( + `Cannot retry as 'jitter' must be <= 1: current value is ${options.jitter}`, + ); } - if (options.jitter > 1) throw new TypeError("jitter is greater than 1"); let attempt = 0; while (true) { diff --git a/async/retry_test.ts b/async/retry_test.ts index 95f09fcbb..e90b34f24 100644 --- a/async/retry_test.ts +++ b/async/retry_test.ts @@ -64,7 +64,7 @@ Deno.test( maxTimeout: 100, }), TypeError, - "minTimeout is greater than maxTimeout", + "Cannot retry as 'minTimeout' must be <= 'maxTimeout': current values 'minTimeout=1000', 'maxTimeout=100'", ); }, ); @@ -78,7 +78,7 @@ Deno.test( maxTimeout: -1, }), TypeError, - "maxTimeout is less than 0", + "Cannot retry as 'maxTimeout' must be positive: current value is -1", ); }, ); @@ -92,7 +92,7 @@ Deno.test( jitter: 2, }), TypeError, - "jitter is greater than 1", + "Cannot retry as 'jitter' must be <= 1: current value is 2", ); }, );