diff --git a/retry/lib/retry.dart b/retry/lib/retry.dart index 10af9ae4..9dd07239 100644 --- a/retry/lib/retry.dart +++ b/retry/lib/retry.dart @@ -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] @@ -121,7 +121,9 @@ final class RetryOptions { Future retry( FutureOr Function() fn, { FutureOr Function(Exception)? retryIf, + @Deprecated('Use `onRetryFailure` instead of `onRetry`') FutureOr Function(Exception)? onRetry, + FutureOr Function(Exception, StackTrace)? onRetryFailure, }) async { var attempt = 0; // ignore: literal_only_boolean_expressions @@ -129,7 +131,7 @@ final class RetryOptions { 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; @@ -137,6 +139,9 @@ final class RetryOptions { if (onRetry != null) { await onRetry(e); } + if (onRetryFailure != null) { + await onRetryFailure(e, st); + } } // Sleep for a delay @@ -178,11 +183,14 @@ Future retry( Duration maxDelay = const Duration(seconds: 30), int maxAttempts = 8, FutureOr Function(Exception)? retryIf, + @Deprecated('Use `onRetryFailure` instead of `onRetry`') FutureOr Function(Exception)? onRetry, + FutureOr 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); diff --git a/retry/test/retry_test.dart b/retry/test/retry_test.dart index 75930633..730aa0f7 100644 --- a/retry/test/retry_test.dart +++ b/retry/test/retry_test.dart @@ -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()); + expect(st, isA()); + }); + }); }); }