Skip to content

Commit df272a5

Browse files
committed
Change default retry() behavior, and bump to 3.0.0
1 parent c50ff41 commit df272a5

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

retry/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v3.0.0
2+
* When `retryIf` is not given, we default to retry any `Exception` thrown.
3+
This is breaking, but provides a more useful default behavior.
4+
15
## v2.0.0
26
* Expect complete API break.
37
* Initial release started from scratch, lifted from code in

retry/lib/retry.dart

+10-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ class RetryOptions {
113113
///
114114
/// At every retry the [onRetry] function will be called (if given). The
115115
/// function [fn] will be invoked at-most [this.attempts] times.
116+
///
117+
/// If no [retryIf] function is given this will retry any for any [Exception]
118+
/// thrown. To retry on an [Error], the error must be caught and _rethrown_
119+
/// as an [Exception].
116120
Future<T> retry<T>(
117121
FutureOr<T> Function() fn, {
118122
FutureOr<bool> Function(Exception) retryIf,
@@ -125,7 +129,8 @@ class RetryOptions {
125129
try {
126130
return await fn();
127131
} on Exception catch (e) {
128-
if (attempt >= maxAttempts || retryIf == null || !(await retryIf(e))) {
132+
if (attempt >= maxAttempts ||
133+
(retryIf != null && !(await retryIf(e)))) {
129134
rethrow;
130135
}
131136
if (onRetry != null) {
@@ -161,6 +166,10 @@ class RetryOptions {
161166
/// );
162167
/// print(response.body);
163168
/// ```
169+
///
170+
/// If no [retryIf] function is given this will retry any for any [Exception]
171+
/// thrown. To retry on an [Error], the error must be caught and _rethrown_
172+
/// as an [Exception].
164173
Future<T> retry<T>(
165174
FutureOr<T> Function() fn, {
166175
Duration delayFactor = const Duration(milliseconds: 200),

retry/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: retry
2-
version: 2.0.0
2+
version: 3.0.0
33
authors:
44
- Jonas Finnemann Jensen <[email protected]>
55
description: |
66
Utility for wrapping an asynchronous function in automatic retry logic with
7-
exponential backoff, useful when making requests over network.
7+
exponential back-off, useful when making requests over network.
88
homepage: https://github.com/google/dart-neats/tree/master/retry
99
repository: https://github.com/google/dart-neats.git
1010
issue_tracker: https://github.com/google/dart-neats/labels/pkg:retry

retry/test/retry_test.dart

+18-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void main() {
5757
final f = r.retry(() {
5858
count++;
5959
throw Exception('Retry will fail');
60-
});
60+
}, retryIf: (e) => false);
6161
await expectLater(f, throwsA(isException));
6262
expect(count, equals(1));
6363
});
@@ -93,6 +93,23 @@ void main() {
9393
expect(count, equals(2));
9494
});
9595

96+
test('retry (no retryIf)', () async {
97+
int count = 0;
98+
final r = RetryOptions(
99+
maxAttempts: 5,
100+
maxDelay: Duration(),
101+
);
102+
final f = r.retry(() {
103+
count++;
104+
if (count == 1) {
105+
throw FormatException('Retry will be okay');
106+
}
107+
return true;
108+
});
109+
await expectLater(f, completion(isTrue));
110+
expect(count, equals(2));
111+
});
112+
96113
test('retry (unhandled on 2nd try)', () async {
97114
int count = 0;
98115
final r = RetryOptions(

0 commit comments

Comments
 (0)