Laravel's Request Exception Truncation Controls: Debug Without the Bloat

HTTP exception messages in Laravel get automatically truncated to prevent log bloat, but this can hide crucial debugging information. New truncation controls let you adjust this behavior globally or per-request.
Understanding the Default Behavior
Laravel automatically truncates RequestException
messages to 120 characters when logging or reporting errors. This prevents your logs from being overwhelmed by massive API error responses, but it can sometimes hide important details you need for debugging:
// This might be truncated to just the first 120 characters
HTTP request returned status code 422:
{"errors":{"email":["The email has already been taken."],"password":["The password must be at least 8 characters."],"phone":["The phone format is invalid."]}} // ... (truncated)
Configuring Global Truncation Behavior
You can now customize this behavior globally in your bootstrap/app.php
file using the new truncation methods:
use Illuminate\Foundation\Configuration\Exceptions;
return Application::configure(basePath: dirname(__DIR__))
->withExceptions(function (Exceptions $exceptions) {
// Increase truncation limit to 240 characters
$exceptions->truncateRequestExceptionsAt(240);
// Or disable truncation entirely
$exceptions->dontTruncateRequestExceptions();
})
->create();
Real-World Example
Consider an e-commerce application that integrates with multiple payment providers and inventory systems. Different APIs return varying levels of detail in their error responses, and you need different truncation strategies based on the criticality of the operation:
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class PaymentService
{
public function processPayment(array $paymentData): array
{
try {
$response = Http::truncateExceptionsAt(500)
->timeout(30)
->post('https://api.payment-provider.com/charge', $paymentData);
return $response->json();
} catch (RequestException $e) {
// Full error details are preserved for payment debugging
Log::error('Payment processing failed', [
'payment_data' => $paymentData,
'error' => $e->getMessage(), // Up to 500 characters
'response_body' => $e->response?->body()
]);
throw $e;
}
}
public function validateInventory(string $productId): bool
{
try {
// Inventory checks can use shorter truncation
$response = Http::truncateExceptionsAt(150)
->get("https://api.inventory.com/check/{$productId}");
return $response->json('available', false);
} catch (RequestException $e) {
// Shorter error messages for less critical operations
Log::warning('Inventory check failed', [
'product_id' => $productId,
'error' => $e->getMessage() // Up to 150 characters
]);
return false;
}
}
public function syncProductCatalog(): void
{
try {
// Bulk operations might need no truncation for debugging
$response = Http::withoutTruncatingExceptions()
->timeout(120)
->post('https://api.catalog.com/bulk-sync', [
'products' => $this->getPendingProducts()
]);
$this->processSyncResults($response->json());
} catch (RequestException $e) {
// Full error details for complex sync operations
Log::error('Catalog sync failed', [
'error' => $e->getMessage(), // No truncation
'products_count' => count($this->getPendingProducts())
]);
throw $e;
}
}
private function getPendingProducts(): array
{
// ...
return [];
}
private function processSyncResults(array $results): void
{
// ...
}
}
This granular control is especially valuable in microservices architectures where different services have varying error response patterns, or when dealing with third-party APIs that return detailed validation errors you need to preserve for debugging.
The per-request truncation methods (truncateExceptionsAt()
and withoutTruncatingExceptions()
) give you surgical control over error visibility without affecting your global logging strategy. You can keep most operations using the default truncation while expanding limits only where you need detailed error information.
Stay Updated with More Laravel Tips
Enjoyed this article? There's plenty more where that came from! Subscribe to our channels to stay updated with the latest Laravel tips, tricks, and best practices: