Skip to content
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

feat: add stacktrace parameter on retry #211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions retry/lib/retry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ final class RetryOptions {
/// Call [fn] retrying so long as [retryIf] return `true` for the exception
/// thrown.
///
/// At every retry the [onRetry] function will be called (if given). The
/// At every retry the [onRetryFailure] function will be called (if given). The
/// function [fn] will be invoked at-most [this.attempts] times.
///
/// If no [retryIf] function is given this will retry any for any [Exception]
Expand All @@ -121,22 +121,27 @@ final class RetryOptions {
Future<T> retry<T>(
FutureOr<T> Function() fn, {
FutureOr<bool> Function(Exception)? retryIf,
@Deprecated('Use `onRetryFailure` instead of `onRetry`')
FutureOr<void> Function(Exception)? onRetry,
FutureOr<void> Function(Exception, StackTrace)? onRetryFailure,
}) async {
var attempt = 0;
// ignore: literal_only_boolean_expressions
while (true) {
attempt++; // first invocation is the first attempt
try {
return await fn();
} on Exception catch (e) {
} on Exception catch (e, st) {
if (attempt >= maxAttempts ||
(retryIf != null && !(await retryIf(e)))) {
rethrow;
}
if (onRetry != null) {
await onRetry(e);
}
if (onRetryFailure != null) {
await onRetryFailure(e, st);
}
}

// Sleep for a delay
Expand Down Expand Up @@ -178,11 +183,14 @@ Future<T> retry<T>(
Duration maxDelay = const Duration(seconds: 30),
int maxAttempts = 8,
FutureOr<bool> Function(Exception)? retryIf,
@Deprecated('Use `onRetryFailure` instead of `onRetry`')
FutureOr<void> Function(Exception)? onRetry,
FutureOr<void> Function(Exception, StackTrace)? onRetryFailure,
}) =>
RetryOptions(
delayFactor: delayFactor,
randomizationFactor: randomizationFactor,
maxDelay: maxDelay,
maxAttempts: maxAttempts,
).retry(fn, retryIf: retryIf, onRetry: onRetry);
).retry(fn,
retryIf: retryIf, onRetry: onRetry, onRetryFailure: onRetryFailure);
13 changes: 13 additions & 0 deletions retry/test/retry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,18 @@ void main() {
await expectLater(f, throwsA(isException));
expect(count, equals(2));
});

test('call onRetryFailure when exception on retry', () async {
var count = 0;
await retry(() {
count++;
if (count == 1) {
throw FormatException('Retry will be okay');
}
}, onRetryFailure: (e, st) {
expect(e, isA<FormatException>());
expect(st, isA<StackTrace>());
});
});
});
}