dart-neats icon indicating copy to clipboard operation
dart-neats copied to clipboard

Retry: Retry If Retruned Object Meets a Condition

Open AhmedNourJamalElDin opened this issue 4 years ago • 7 comments

Hi,

I'd like to retry if the returned object meets a condition.

Example:

      final response = await retry(
      // Make a GET request
      () => http.get('https://google.com'),
      // Retry on Exception
      retryIf: (e) => e is SocketException,
      retryIfReturnedObject: (o) => o == "retry-please"
  );

is this possible? Thanks

AhmedNourJamalElDin avatar Jun 01 '20 14:06 AhmedNourJamalElDin

IMO, it would be ideal to the function called throw an Exception instead..

But I can see that something like package:http won't throw on 5xx responses, you if you want to retry those, this would be pretty neat.

jonasfj avatar Jul 14 '20 13:07 jonasfj

quick note: I wonder if we could call it retryIfValue instead of retryIfReturnedObject which rather long... Or maybe retryOnReturnIf, other good names?

jonasfj avatar Jul 14 '20 13:07 jonasfj

retryValueIf might be solid..

jonasfj avatar Jul 14 '20 13:07 jonasfj

Maybe we could just rename the parameters to be more descriptive, for example:

final response = await retry(
    // Make a GET request
    () => http.get('https://google.com'),
    // Retry on Exception
    retryOnException: (e) => e is SocketException,
    retryOnSuccess: (o) => o == "retry-please"
);

Ascenio avatar Sep 07 '20 01:09 Ascenio

That's exactly the thing I missed/ I'd like to suggest!

I like @Ascenio's idea to rename the parameters (although it is a breaking change, which requires users to adjust their code). I'd prefer an even 'simpler' renaming like the following:

final response = await retry(
    // Make a GET request
    () => http.get('https://google.com'),
    // Retry on Exception
    retryOnException: (e) => e is SocketException, // or could also be "retryOnError" - sounds even better to me
    retryIf: (o) => o == "retry-please"

);

This would have been the 'intuitive' API I would have expected on my first use. Overall thanks for considering this and BR

Sebsen avatar Dec 13 '20 19:12 Sebsen

How about: retryIf (for exceptions) retryOn (for values)

We don't break anything this way

nwparker avatar Jun 05 '21 18:06 nwparker

any news ?

rajada1 avatar Mar 07 '22 14:03 rajada1