Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw Error inside onRetry #79

Open
yokomotod opened this issue Aug 2, 2021 · 1 comment
Open

throw Error inside onRetry #79

yokomotod opened this issue Aug 2, 2021 · 1 comment

Comments

@yokomotod
Copy link

Throwing an error in onRetry seems like a weird behavior to me.

const retry = require("async-retry");

(async () => {
  try {
    await retry(
      (bail, attempt) => {
        throw new Error(`inside error at ${attempt}`);
      },
      {
        onRetry: (e, attempt) => {
          console.error(e.message);
          throw new Error(`onRetry error at ${attempt}`);
        },
      },
    );
  } catch (e) {
    console.error(e.message);
  }
})();

Result:

inside error at 1
onRetry error at 1
inside error at 2
/home/xxxxx/playground.js:12
          throw new Error(`onRetry error at ${attempt}`);
          ^

Error: onRetry error at 2
    at Object.onRetry (/home/xxxxx/playground.js:12:17)
    at onError (/home/xxxxx/node_modules/async-retry/lib/index.js:33:17)
    at RetryOperation.runAttempt [as _fn] (/home/xxxxx/node_modules/async-retry/lib/index.js:43:9)
    at Timeout._onTimeout (/home/xxxxx/node_modules/retry/lib/retry_operation.js:81:10)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7)

I expected that it caught once without retry like:

inside error at 1
onRetry error at 1
(finish)

otherwise, with retry like:

inside error at 1
inside error at 2
inside error at 3
inside error at 4
inside error at 5
inside error at 6
onRetry error at 6
(finish)

Is this a bug?

@jonniesweb
Copy link

Too bad this library isn't being maintained anymore. Ended up fixing this by wrapping the function to be retried with my own try/catch that calls what I had in onRetry, eg.

  async withRetry(fn) {
    let attempt = 0;
    const wrapper = async (...params) => {
      try {
        attempt++;
        return await fn(...params);
      } catch (e) {
        console.warn(`Retrying attempt ${attempt}`, e);

        // what I would have had in my onRetry
        if (e.code == 401 || e.code == 400) {
          await maybeThrowsError();
        }

        // always throw original error
        throw e;
      }
    }

    return await retry(wrapper, { retries: 1 });
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants