Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Catchable queued closures in Laravel 8

One of the handiest features of Laravel is the ability to dispatch Closures to queues. This is because you don’t always need to create a job class for simple tasks such as sending emails.

So, for instance, we want to dispatch the email sending the part to the queue, we can do it like so.

$user = App\User::find(1);

dispatch(function () use ($user) {
    Mail::to($user->email)->send(new \App\Mail\OrderShipped);
});

Now, this is already a nice feature to have but it was missing a critical thing. The ability to catch errors if something goes wrong while executing the queued closures.

The catch method

Laravel 8 tries to solve this by adding this ability using a new catch method which can be used on the dispatch method. The method accepts a Closure which will be executed if the queued Closure fails after exhausting all of your queue’s configured retry attempts defined by retry_after on the queue driver’s configuration in your config/queue.php file.

So, the previous example can be rewritten with catch like so.

use Throwable;

dispatch(function () use ($podcast) {
    Mail::to($user->email)->send(new \App\Mail\OrderShipped);
})->catch(function (Throwable $e) {
    // This job has failed...
});

As you can tell, the catch Closure receives the instance of the Throwable interface which can be used to identify the error occurred during Closure’s execution in the queue.

This is especially useful in logging exception information to log files or to external services to know what led the queue to the failed state.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?