Commit 49d705d

Anton Golub <antongolub@antongolub.com>
2025-06-19 12:47:03
refactor: improve `retry()` error description (#1233)
1 parent b1325c1
build/cli.js
build/index.cjs
@@ -52,7 +52,6 @@ var import_vendor2 = require("./vendor.cjs");
 __reExport(index_exports, require("./core.cjs"), module.exports);
 
 // src/goods.ts
-var import_node_assert = __toESM(require("assert"), 1);
 var import_node_readline = require("readline");
 var import_node_stream = require("stream");
 var import_core = require("./core.cjs");
@@ -177,11 +176,11 @@ function stdin() {
 function retry(count, d, cb) {
   return __async(this, null, function* () {
     if (typeof d === "function") return retry(count, 0, d);
-    (0, import_node_assert.default)(cb);
+    if (!cb) throw new Error("Callback is required for retry");
     const total = count;
-    const gen = typeof d === "object" ? d : function* () {
-      while (true) yield (0, import_util.parseDuration)(d);
-    }();
+    const gen = typeof d === "object" ? d : function* (d2) {
+      while (true) yield d2;
+    }((0, import_util.parseDuration)(d));
     let attempt = 0;
     let lastErr;
     while (count-- > 0) {
src/goods.ts
@@ -12,7 +12,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-import assert from 'node:assert'
 import { createInterface } from 'node:readline'
 import { Readable } from 'node:stream'
 import { $, within, ProcessOutput, type ProcessPromise } from './core.ts'
@@ -178,16 +177,15 @@ export async function retry<T>(
   cb?: () => T
 ): Promise<T> {
   if (typeof d === 'function') return retry(count, 0, d)
-
-  assert(cb)
+  if (!cb) throw new Error('Callback is required for retry')
 
   const total = count
   const gen =
     typeof d === 'object'
       ? d
-      : (function* () {
-          while (true) yield parseDuration(d)
-        })()
+      : (function* (d) {
+          while (true) yield d
+        })(parseDuration(d))
 
   let attempt = 0
   let lastErr: unknown
test/goods.test.ts
@@ -147,6 +147,15 @@ describe('goods', () => {
       }
     })
 
+    test('throws err on empty callback', async () => {
+      try {
+        // @ts-ignore
+        await retry(5)
+      } catch (e) {
+        assert.match(e.message, /Callback is required for retry/)
+      }
+    })
+
     test('supports expBackoff', async () => {
       const result = await retry(5, expBackoff('10ms'), () => {
         if (Math.random() < 0.1) throw new Error('fail')
.size-limit.json
@@ -29,7 +29,7 @@
       "build/globals.js",
       "build/deno.js"
     ],
-    "limit": "811.90 kB",
+    "limit": "811.85 kB",
     "brotli": false,
     "gzip": false
   },