diff --git a/src/retry.php b/src/retry.php index 17fc7dd..49eede1 100644 --- a/src/retry.php +++ b/src/retry.php @@ -4,16 +4,18 @@ class FailingTooHardException extends \Exception {} -function retry($retries, callable $fn) +function retry($retries, callable $fn, callable $onError = null) { - beginning: - try { - return $fn(); - } catch (\Exception $e) { - if (!$retries) { - throw new FailingTooHardException('', 0, $e); + do + { + try { + return $fn(); + } catch (\Exception $e) {} + if ($onError) { + $onError($e); } - $retries--; - goto beginning; } + while ($retries--); + throw new FailingTooHardException('', 0, $e); } +