dart-neats
dart-neats copied to clipboard
Retry: Retry If Retruned Object Meets a Condition
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
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.
quick note: I wonder if we could call it retryIfValue
instead of retryIfReturnedObject
which rather long...
Or maybe retryOnReturnIf
, other good names?
retryValueIf
might be solid..
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"
);
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
How about:
retryIf
(for exceptions)
retryOn
(for values)
We don't break anything this way
any news ?