Skip to content

feat: Add a callback to be called on transaction failure#55338

Merged
taylorotwell merged 2 commits into
laravel:12.xfrom
dshafik:add-on-failure-callback-transactions
Apr 9, 2025
Merged

feat: Add a callback to be called on transaction failure#55338
taylorotwell merged 2 commits into
laravel:12.xfrom
dshafik:add-on-failure-callback-transactions

Conversation

@dshafik

@dshafik dshafik commented Apr 9, 2025

Copy link
Copy Markdown
Contributor

This PR adds a new argument to DB::transaction() that allows you to pass in a callback to be executed when the transaction fails:

DB::transaction(function () {
 // do DB stuff
}, onFailureCallback: function () {
    Notification::send($admin, new SomethingImportantBroke());
});

@dshafik dshafik force-pushed the add-on-failure-callback-transactions branch from b668a42 to 65fcd2d Compare April 9, 2025 11:40
@dshafik dshafik force-pushed the add-on-failure-callback-transactions branch from 65fcd2d to cb518a9 Compare April 9, 2025 11:44
@taylorotwell taylorotwell merged commit 7d65ce0 into laravel:12.x Apr 9, 2025
@taylorotwell

Copy link
Copy Markdown
Member

Thanks 👍

@taylorotwell

Copy link
Copy Markdown
Member

I need to revert this one since it's a breaking change on that interface. Sorry!

@negoziator

negoziator commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

Thank you @taylorotwell ! I was just about to comment on it, because of e.g this❤️

@dshafik

dshafik commented Apr 16, 2025

Copy link
Copy Markdown
Contributor Author

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.

@negoziator

Copy link
Copy Markdown
Contributor

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 😀

@rodrigopedra

Copy link
Copy Markdown
Contributor

@dshafik please consider sending it to master, it is indeed a nice contribution

@negoziator

Copy link
Copy Markdown
Contributor

@dshafik I was thinking.

Maybe it could be possible to implement a new function e.g transactionWithCallback() 🤔

I didn't think it through quite yet - but i really like the idea of onFailure() callback in the trasaction

@alcaeus

alcaeus commented Apr 17, 2025

Copy link
Copy Markdown
Contributor

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?

@moe-mizrak

Copy link
Copy Markdown

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();

https://github.com/moe-mizrak/transaction-builder

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