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

Conversation

sethsandaru
Copy link
Contributor

@sethsandaru sethsandaru commented Apr 6, 2024

Hi team,

rescue has been catching Throwable for a while but not for retry. An Error would easily break the retry helper.

This PR updates that.

@@ -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

@antonkomarev
Copy link
Contributor

@sethsandaru what is the reason to retry errors? Errors happen on programming mistakes. For example, if there is TypeError, you should just interrupt all execution and go fix a code.

@taylorotwell taylorotwell merged commit 92fe58a into laravel:11.x Apr 7, 2024
19 of 30 checks passed
@antonkomarev
Copy link
Contributor

antonkomarev commented Apr 7, 2024

@taylorotwell @driesvints are you sure this is a good idea? Error types are not meant to be retried. It's a sign of a persistent issue in the code, not runtime issues which may fail eventually.

For example, I will pass the string to the method where the param must be an integer. If we will retry this call 10 times, this code will fail 10 times.

And as @nunomaduro mentioned earlier, this is a breaking change.

@sethsandaru
Copy link
Contributor Author

sethsandaru commented Apr 8, 2024

@sethsandaru what is the reason to retry errors? Errors happen on programming mistakes. For example, if there is TypeError, you should just interrupt all execution and go fix a code.

@antonkomarev from my projects, I want to retry at least 1 regardless of Errors/Exceptions (libraries, low-level, etc). We'd never know when Error/Exception happens and there would be "Error" on runtime (3rd-party stuff, WS calls, internal, etc)

This change would somehow make retry consistent with the retry from Queue Job command - if you specify the retry times, it will always retry on any Error/Exception

Stuff like you mentioned "TypeError", it's already been battle-tested via unit testing & strict validation in my project. I guess other projects would follow this as well.

@antonkomarev
Copy link
Contributor

@sethsandaru The queue needs to handle it to mark job as failed. Because code may be changed between jobs handling. And we could repeat execution later when code will be fixed.

On the other side retry helper will never receive a modified code between tries, because all iterations are done in a single process.

@driesvints
Copy link
Member

Maybe function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null, string $errorType = Exception::class) so you can pass whatever you want to $errorType could help?

retry(3, fn () => 'foo', errorType: Throwable::class)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants