diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 5cf93d7a9341..be59c1fe353b 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -278,7 +278,7 @@ function preg_replace_array($pattern, array $replacements, $subject) * @param callable|null $when * @return mixed * - * @throws \Exception + * @throws \Throwable */ function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null) { @@ -298,7 +298,7 @@ function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null) try { return $callback($attempts); - } catch (Exception $e) { + } catch (Throwable $e) { if ($times < 1 || ($when && ! $when($e))) { throw $e; } diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index 6d1149bdeb04..6183449af631 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -5,6 +5,7 @@ use ArrayAccess; use ArrayIterator; use Countable; +use Error; use Illuminate\Contracts\Support\Htmlable; use Illuminate\Support\Env; use Illuminate\Support\Optional; @@ -1008,6 +1009,29 @@ public function testRetryWithBackoff() ]); } + public function testRetryWithAThrowableBase() + { + Sleep::fake(); + + $attempts = retry(2, function ($attempts) { + if ($attempts > 1) { + return $attempts; + } + + throw new Error('This is an error'); + }, 100); + + // Make sure we made two attempts + $this->assertEquals(2, $attempts); + + // Make sure we waited 100ms for the first attempt + Sleep::assertSleptTimes(1); + + Sleep::assertSequence([ + Sleep::usleep(100_000), + ]); + } + public function testTransform() { $this->assertEquals(10, transform(5, function ($value) {