Skip to content

[12.x] FailOnException middleware #10508

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

Merged
merged 5 commits into from
Jun 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,64 @@ $this->fail('Something went wrong.');
> [!NOTE]
> For more information on failed jobs, check out the [documentation on dealing with job failures](#dealing-with-failed-jobs).

<a name="fail-jobs-on-exceptions"></a>
#### Failing Jobs on Specific Exceptions

The `FailOnException` [job middleware](#job-middleware) allows you to short-circuit retries when specific exceptions are thrown. This allows retrying on transient exceptions such as external API errors, but failing the job permanently on persistent exceptions, such as a user's permissions being revoked:

```php
<?php

namespace App\Jobs;

use App\Models\User;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\FailOnException;
use Illuminate\Support\Facades\Http;

class SyncChatHistory implements ShouldQueue
{
use InteractsWithQueue;

public $tries = 3;

/**
* Create a new job instance.
*/
public function __construct(
public User $user,
) {}

/**
* Execute the job.
*/
public function handle(): void
{
$user->authorize('sync-chat-history');

$response = Http::throw()->get(
"https://chat.laravel.test/?user={$user->uuid}
");


// ...
}

/**
* Get the middleware the job should pass through.
*/
public function middleware(): array
{
return [
new FailOnException([AuthorizationException::class])
];
}
}
```

<a name="job-batching"></a>
## Job Batching

Expand Down