Skip to content

Commit 7656ab8

Browse files
[12.x] FailOnException middleware (#10508)
* FailOnException middleware * Update queues.md * Update queues.md * formatting * wip --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 013514a commit 7656ab8

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

queues.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,64 @@ $this->fail('Something went wrong.');
14281428
> [!NOTE]
14291429
> For more information on failed jobs, check out the [documentation on dealing with job failures](#dealing-with-failed-jobs).
14301430
1431+
<a name="fail-jobs-on-exceptions"></a>
1432+
#### Failing Jobs on Specific Exceptions
1433+
1434+
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:
1435+
1436+
```php
1437+
<?php
1438+
1439+
namespace App\Jobs;
1440+
1441+
use App\Models\User;
1442+
use Illuminate\Auth\Access\AuthorizationException;
1443+
use Illuminate\Contracts\Queue\ShouldQueue;
1444+
use Illuminate\Foundation\Queue\Queueable;
1445+
use Illuminate\Queue\InteractsWithQueue;
1446+
use Illuminate\Queue\Middleware\FailOnException;
1447+
use Illuminate\Support\Facades\Http;
1448+
1449+
class SyncChatHistory implements ShouldQueue
1450+
{
1451+
use InteractsWithQueue;
1452+
1453+
public $tries = 3;
1454+
1455+
/**
1456+
* Create a new job instance.
1457+
*/
1458+
public function __construct(
1459+
public User $user,
1460+
) {}
1461+
1462+
/**
1463+
* Execute the job.
1464+
*/
1465+
public function handle(): void
1466+
{
1467+
$user->authorize('sync-chat-history');
1468+
1469+
$response = Http::throw()->get(
1470+
"https://chat.laravel.test/?user={$user->uuid}
1471+
");
1472+
1473+
1474+
// ...
1475+
}
1476+
1477+
/**
1478+
* Get the middleware the job should pass through.
1479+
*/
1480+
public function middleware(): array
1481+
{
1482+
return [
1483+
new FailOnException([AuthorizationException::class])
1484+
];
1485+
}
1486+
}
1487+
```
1488+
14311489
<a name="job-batching"></a>
14321490
## Job Batching
14331491

0 commit comments

Comments
 (0)