Customize Laravel error reporting to remove stack trace
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Customize Laravel Error Reporting to Remove Stack Traces
Dealing with exceptions in a Laravel application is inevitable. When something goes wrong, seeing a detailed stack trace can be useful for debugging during development, but it quickly becomes noise when you are monitoring production logs or want to present a clean error message to an end-user. Many developers run into the issue of excessive stack traces cluttering console output or log files.
The good news is that Laravel provides a powerful mechanism to intercept and customize how exceptions are handled, allowing you to suppress verbose stack traces while still ensuring proper logging occurs behind the scenes. This guide will walk you through exactly how to modify the default error reporting in Laravel to achieve cleaner output.
Understanding the Exception Handler
In Laravel, all exception handling is managed by the app/Exceptions/Handler.php file. This class contains the logic that determines what happens when an uncaught exception occurs—whether it gets logged, displayed to the user, or thrown further up the chain. To remove stack traces, we need to override the default methods used for rendering these exceptions.
By default, Laravel renders detailed exception information when an HTTP request fails. Our goal is to intercept this process and replace the verbose trace with a more controlled, application-specific message.
Implementing Custom Error Rendering
To effectively remove or modify stack traces, you will primarily focus on overriding the render() method within your custom Exception Handler. This method is responsible for formatting the response sent back to the client (or logged, depending on context).
Here is a practical example of how you can modify your handler to suppress detailed error messages:
<?php
namespace App\Exceptions;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Console\Kernel;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Symfony\Component\HttpKernel\Exception\HttpException;
class Handler extends ExceptionHandler
{
/**
* A list of exceptions to report.
*
* @var array<int, class-string>
*/
protected $dontReport = [
//
];
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*/
public function render($request, Throwable $exception)
{
// Check if we are in an environment where we want to suppress traces (e.g., production)
if ($exception instanceof \Exception && !app()->is('local')) {
// Log the full details internally for debugging purposes (best practice!)
\Log::error("Caught Exception: " . $exception->getMessage(), ['trace' => $exception->getTraceAsString()]);
// Return a generic, user-friendly response instead of the full stack trace
return response()->json([
'message' => 'An unexpected error occurred. Please try again later.',
'code' => 500
], 500);
}
// For all other exceptions (like validation errors or 404s), let Laravel handle the default rendering
return parent::render($request, $exception);
}
}
Best Practices for Suppression
Notice the strategy used above. Instead of simply throwing away the exception information, a senior developer approach is to log the full context internally (using \Log::error with the trace) while presenting a sanitized message to the end-user. This ensures that developers still have the necessary debug information in your logs, adhering to best practices for application monitoring which is critical when building robust systems like those supported by Laravel (see how core components function on https://laravelcompany.com).
Conclusion
By customizing the render() method in app/Exceptions/Handler.php, you gain complete control over the error reporting lifecycle in your Laravel application. This technique allows you to balance the need for detailed debugging information on the server side with the requirement for clean, secure, and non-verbose error messages for end-users. Remember, always prioritize logging the full context internally while providing generic feedback externally.