Frustrated by truncated HTTP client exceptions obscuring crucial debugging information in bug reports or Sentry? Laravel's default truncation of HTTP responses can hide vital clues about request failures.
This problem is solved in Laravel 11.35! Two new methods offer precise control over exception truncation. Simply add them to your bootstrap/app.php
:
// bootstrap/app.php use Illuminate\Http\Client\RequestException; return Application::configure(basePath: dirname(__DIR__)) // ... ->withExceptions(function (Exceptions $exceptions) { $exceptions->dontTruncateRequestExceptions(); // Completely disable truncation // Or... $exceptions->truncateRequestExceptionsAt(260); // Set a custom truncation length })->create();
Now, HTTP client exceptions will either remain fully intact or be truncated to your specified length (longer than the default). You retain full control over exception handling and logging:
try { $response = Http::throws()->get('https://api.example.com/some-error'); // ... } catch (\Illuminate\Http\Client\RequestException $e) { Log::error('HTTP Error', [ 'message' => $e->getMessage(), // Truncated or not, depending on your setting 'response' => $e->response->json(), 'status' => $e->response->status(), // ... ]); }
For detailed information on exception handling and truncation within Laravel's HTTP client, consult the official Laravel documentation.
Special thanks to Steve Bauman (Pull Request #53734) for this valuable improvement in Laravel 11.35.0!
The above is the detailed content of Customize the Truncation of HTTP Client Request Exceptions. For more information, please follow other related articles on the PHP Chinese website!