Home > Backend Development > PHP Tutorial > Customize the Truncation of HTTP Client Request Exceptions

Customize the Truncation of HTTP Client Request Exceptions

Robert Michael Kim
Release: 2025-03-06 02:39:09
Original
235 people have browsed it

Customize the Truncation of HTTP Client Request Exceptions

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();
Copy after login

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(),
        // ...
    ]);
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template