-
Notifications
You must be signed in to change notification settings - Fork 11.4k
feat: Add a callback to be called on transaction failure #55338
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
feat: Add a callback to be called on transaction failure #55338
Conversation
b668a42
to
65fcd2d
Compare
65fcd2d
to
cb518a9
Compare
Thanks 👍 |
…thod Fix compatibility with Laravel 12.9.0 laravel/framework#55338
I need to revert this one since it's a breaking change on that interface. Sorry! |
Thank you @taylorotwell ! I was just about to comment on it, because of e.g this❤️ |
I was going to do that today also, sorry about that! I didn't consider there were outside consumers of the interface, my bad! I'll be more careful in future. |
I think it was very good thinking even though 😀 |
@dshafik please consider sending it to |
@dshafik I was thinking. Maybe it could be possible to implement a new function e.g I didn't think it through quite yet - but i really like the idea of onFailure() callback in the trasaction |
When looking at the feature in more detail trying to replicate it for the MongoDB integration (which has slightly different transaction handling due to how transactions work in MongoDB), I wasn't quite sure why the callback was preferable to a try-catch block: try {
DB::transaction(function () {
// TODO: World peace
});
} catch (Throwable $e) {
// TODO: Save the world
} I'm sure I'm missing some part of this, maybe @dshafik could help me understand why the above isn't sufficient? |
I created a lightweight package which can be extended (since it is chainable instead of handling it with params): Basically it wraps DB::transaction() so that we can use it as $result = TransactionBuilder::make()
->attempts(3) // number of attempts
->run(function () {
// your transaction logic
return 'test';
})
->onFailure(function ($exception) {
// your onFailure callback logic
logger()->error($exception->getMessage());
})
->result(); |
This PR adds a new argument to
DB::transaction()
that allows you to pass in a callback to be executed when the transaction fails: