-
Notifications
You must be signed in to change notification settings - Fork 89
orElse callback when retries are exhausted #167
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,10 +118,15 @@ class RetryOptions { | |
| /// If no [retryIf] function is given this will retry any for any [Exception] | ||
| /// thrown. To retry on an [Error], the error must be caught and _rethrown_ | ||
| /// as an [Exception]. | ||
| /// | ||
| /// If [orElse] is given and the [attemps] are exhausted by retryable | ||
| /// exceptions (either by default or via [retryIf]), the function will | ||
| /// return the result of [orElse]. | ||
| Future<T> retry<T>( | ||
| FutureOr<T> Function() fn, { | ||
| FutureOr<bool> Function(Exception)? retryIf, | ||
| FutureOr<void> Function(Exception)? onRetry, | ||
| FutureOr<T> Function(Exception)? orElse, | ||
jonasfj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) async { | ||
| var attempt = 0; | ||
| // ignore: literal_only_boolean_expressions | ||
|
|
@@ -132,6 +137,10 @@ class RetryOptions { | |
| } on Exception catch (e) { | ||
| if (attempt >= maxAttempts || | ||
| (retryIf != null && !(await retryIf(e)))) { | ||
| if (orElse != null) { | ||
| return await orElse(e); | ||
| } | ||
|
Comment on lines
+140
to
+142
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two kinds of exceptions here:
There are two reasons a call to
Do we think it's right to call
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, this is an old one that is still open :) I think we should only have an alternative callback for (ii), and with that in mind,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
|
||
| rethrow; | ||
| } | ||
| if (onRetry != null) { | ||
|
|
@@ -171,6 +180,10 @@ class RetryOptions { | |
| /// If no [retryIf] function is given this will retry any for any [Exception] | ||
| /// thrown. To retry on an [Error], the error must be caught and _rethrown_ | ||
| /// as an [Exception]. | ||
| /// | ||
| /// If [orElse] is given and the [attemps] are exhausted by retryable | ||
| /// exceptions (either by default or via [retryIf]), the function will | ||
| /// return the result of [orElse]. | ||
| Future<T> retry<T>( | ||
| FutureOr<T> Function() fn, { | ||
| Duration delayFactor = const Duration(milliseconds: 200), | ||
|
|
@@ -179,10 +192,16 @@ Future<T> retry<T>( | |
| int maxAttempts = 8, | ||
| FutureOr<bool> Function(Exception)? retryIf, | ||
| FutureOr<void> Function(Exception)? onRetry, | ||
| FutureOr<T> Function(Exception)? orElse, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. documentation needed
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
| }) => | ||
| RetryOptions( | ||
| delayFactor: delayFactor, | ||
| randomizationFactor: randomizationFactor, | ||
| maxDelay: maxDelay, | ||
| maxAttempts: maxAttempts, | ||
| ).retry(fn, retryIf: retryIf, onRetry: onRetry); | ||
| ).retry( | ||
| fn, | ||
| retryIf: retryIf, | ||
| onRetry: onRetry, | ||
| orElse: orElse, | ||
| ); | ||
Uh oh!
There was an error while loading. Please reload this page.