Overview
Retry rules and fallbacks live right beside the call they protect. Wrap a closure or class, choose which failures to retry, and define a fallback for when retries run out.
Features
- Retry selected exceptions with fixed delays, backoff, or jitter.
- Run fallback handlers in order until one succeeds.
- Hook into retries, success, and failure.
- Wrap individual stages in Laravel’s Pipeline.
- Retry transient provider failures with
Attempt::ai()and the Laravel AI SDK.
Install
Requires PHP 8.4 or later and Laravel 12 or 13.
composer require yannelli/attempt
Optional config:
php artisan vendor:publish --tag="attempt-config"
Example
Here, PrimaryApi gets up to four tries, with exponential backoff between retries. If every call fails, BackupApi runs:
use Yannelli\Attempt\Facades\Attempt;
$result = Attempt::try(PrimaryApi::class)
->retry(3)
->exponentialBackoff(base: 100, max: 5000)
->fallback(BackupApi::class)
->thenReturn();
retry(3) allows three retries after the first call. Swap thenReturn() for run() to get an AttemptResult with the result, exception, and execution count.
