Skip to content

Commit

Permalink
Added attempts and tries to exception handler callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jun 25, 2019
1 parent 68633ae commit 1ade9bf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function retry($tries, callable $operation, callable $onError = null)
}

if ($onError) {
if (($result = $onError($exception)) instanceof Promise) {
if (($result = $onError($exception, $attempts, $tries)) instanceof Promise) {
$result = yield $result;
}

Expand Down
6 changes: 4 additions & 2 deletions test/RetryAsyncTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ public function testErrorCallbackAsync()
$outerException = $innerException = null;

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

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

self::assertSame($innerException, $exception);
self::assertSame(1, $attempts);
self::assertSame(2, $tries);
});
} catch (FailingTooHardException $outerException) {
}
Expand Down
8 changes: 7 additions & 1 deletion test/RetryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public function testFailingTooHard()
self::assertSame($tries, $invocations);
}

/**
* Tests that an error callback receives the exception thrown by the operation, the current attempt and maximum
* number of attempts.
*/
public function testErrorCallback()
{
$invocations = $errors = 0;
Expand All @@ -84,10 +88,12 @@ public function testErrorCallback()
++$invocations;

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

self::assertSame($innerException, $exception);
self::assertSame(1, $attempts);
self::assertSame(2, $tries);
});
} catch (FailingTooHardException $outerException) {
}
Expand Down

0 comments on commit 1ade9bf

Please sign in to comment.