Skip to content

Commit 1ade9bf

Browse files
committed
Added attempts and tries to exception handler callback.
1 parent 68633ae commit 1ade9bf

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/retry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function retry($tries, callable $operation, callable $onError = null)
4141
}
4242

4343
if ($onError) {
44-
if (($result = $onError($exception)) instanceof Promise) {
44+
if (($result = $onError($exception, $attempts, $tries)) instanceof Promise) {
4545
$result = yield $result;
4646
}
4747

test/RetryAsyncTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,16 @@ public function testErrorCallbackAsync()
104104
$outerException = $innerException = null;
105105

106106
try {
107-
\ScriptFUSION\Retry\retryAsync($tries = 3, static function () use (&$invocations, &$innerException) {
107+
\ScriptFUSION\Retry\retryAsync($tries = 2, static function () use (&$invocations, &$innerException) {
108108
++$invocations;
109109

110110
throw $innerException = new \RuntimeException;
111-
}, function (\Exception $exception) use (&$innerException, &$errors) {
111+
}, static function (\Exception $exception, int $attempts, int $tries) use (&$innerException, &$errors) {
112112
++$errors;
113113

114114
self::assertSame($innerException, $exception);
115+
self::assertSame(1, $attempts);
116+
self::assertSame(2, $tries);
115117
});
116118
} catch (FailingTooHardException $outerException) {
117119
}

test/RetryTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function testFailingTooHard()
7474
self::assertSame($tries, $invocations);
7575
}
7676

77+
/**
78+
* Tests that an error callback receives the exception thrown by the operation, the current attempt and maximum
79+
* number of attempts.
80+
*/
7781
public function testErrorCallback()
7882
{
7983
$invocations = $errors = 0;
@@ -84,10 +88,12 @@ public function testErrorCallback()
8488
++$invocations;
8589

8690
throw $innerException = new \RuntimeException;
87-
}, function (\Exception $exception) use (&$innerException, &$errors) {
91+
}, static function (\Exception $exception, int $attempts, int $tries) use (&$innerException, &$errors) {
8892
++$errors;
8993

9094
self::assertSame($innerException, $exception);
95+
self::assertSame(1, $attempts);
96+
self::assertSame(2, $tries);
9197
});
9298
} catch (FailingTooHardException $outerException) {
9399
}

0 commit comments

Comments
 (0)