Your Guide to Laravel Excellence

How to Handle Job Failures in Laravel 12 with FailOnException

How to Handle Job Failures in Laravel 12 with FailOnException

Streamline your job exception logic with Laravel's FailOnException middleware – no more boilerplate try/catch blocks!

Introduction

In Laravel 12, handling job exceptions is cleaner than ever thanks to the FailOnException middleware. Previously, you'd often write verbose try/catch blocks inside your queued job's handle() method.

Now, with FailOnException, you can move exception handling out of the method and into middleware — resulting in more maintainable and readable code.

The Traditional Way (Verbose)

Here’s the older approach many Laravel developers use:

public function handle(TranslateContent $action): void
{
    try {
        $action->handle($this->content);
    } catch (Throwable $exception) {
        if ($exception instanceof InvalidContentFormatException) {
            $this->fail($exception);
            return;
        }

        throw $exception;
    }
}

Problems with the Traditional Approach

  • Repetitive boilerplate across jobs.
  • Obscures the business logic (main handle() intent).
  • Easy to forget to rethrow exceptions or call $this->fail() properly.

The Laravel 12 Way (Elegant)

Here’s the cleaner version using FailOnException:

public function handle(TranslateContent $action): void
{
    $action->handle($this->content);
}

Then define the middleware:

public function middleware(): array
{
    return [
        new FailOnException([InvalidContentFormatException::class]),
    ];
}

Final Example Class

<?php

use DateTime;
use Throwable;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Middleware\FailOnException;

class ProcessContentTranslation implements ShouldQueue
{
    use Dispatchable, Queueable;

    public function __construct(private readonly Content $content) {}

    public function handle(TranslateContent $action): void
    {
        $action->handle($this->content);
    }

    public function retryUntil(): DateTime
    {
        return now()->addHours(5);
    }

    public function middleware(): array
    {
        return [
            new FailOnException([InvalidContentFormatException::class]),
        ];
    }
}

Recommeded Posts

How to Create XML from an Array Using Spatie(array-to-xml) in Laravel

How to Create XML from an Array Using Spatie(array-to-xml) in Laravel

Learn how to convert an array into XML format using the Spatie array-to-xml package in Laravel. This easy guide walks you through the steps to set up the package and create XML files from your data quickly.

2 months ago Read article →
Infinite Scroll in Laravel Using jQuery and AJAX

Infinite Scroll in Laravel Using jQuery and AJAX

Infinite Scroll in Laravel Using jQuery and AJAX

2 months ago Read article →
Laravel 11 Stopping Validation on First Failure

Laravel 11 Stopping Validation on First Failure

Laravel 11 Stopping Validation on First Failure

2 months ago Read article →
New in Laravel 12: Eager Loading, Attribute Scopes, and fromJson

New in Laravel 12: Eager Loading, Attribute Scopes, and fromJson

Discover the new features in Laravel 12! Learn about automatic eager loading, easier query scopes with PHP attributes, and the new Collection::fromJson() method

2 months ago Read article →