Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] retry func - catch "Throwable" instead of Exception #50944

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -298,7 +298,7 @@ function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null)

try {
return $callback($attempts);
} catch (Exception $e) {
} catch (Throwable $e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, so needs to target 12.x. Before doing anything, wait for Taylor's review.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, good to know 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taylorotwell please have a look and let us know if this should be targeted to master

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sethsandaru , why not this?

catch (Exception|Throwable $e) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devajmeireles it's unnecessary to do so, Exception implements Throwable and using union, it still introduces breaking changes

if ($times < 1 || ($when && ! $when($e))) {
throw $e;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Loading