connor4312 / cockatiel

🐦 A resilience and fault-handling library. Supports Backoffs, Retries, Circuit Breakers, Timeouts, Bulkhead Isolation, and Fallbacks.
MIT License
1.59k stars 50 forks source link

Retry policy returns the last filtered result instead of throwing. #71

Closed ADIX7 closed 1 year ago

ADIX7 commented 1 year ago

I try to wrap a fetch api call to a retry policy. As fetch does not throw error on a failed request, I use the result filter. It does work but after the last retry it does not throw the result as an error instead it just returns it. If an error would happen it would have been thrown. I expect that a result that has been identified should be thrown just like an error.

This codesnippet retires failed request as expected but after the last retry returns the result of given request instead of throwing it as an error.

const settingsPolicy = retry(handleWhenResult(r => !r.ok), { maxAttempts: 10, backoff: new IterableBackoff([100, 500, 1000]) });

const settingsRequest = await settingsPolicy.execute(async () =>
  // eslint-disable-next-line no-return-await
  await fetch(`${serviceUrl}/customendpoint`, { headers })
);
connor4312 commented 1 year ago

This is as designed, and is documented here: https://github.com/connor4312/cockatiel#retryexecutefn-signal