Error Handling in laravel 11
If you need to completely overwrite the exception handler class, you may use the withSingletons
method in your application's bootstrap/app.php
to register which class will be handled when Laravel tries to resolve \Illuminate\Contracts\Debug\ExceptionHandler::class
contract
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withSingletons([
\Illuminate\Contracts\Debug\ExceptionHandler::class
=> \App\Exceptions\Handler::class,
])
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})
->create();
If you need to report different types of exceptions in different ways, you may use the report
exception method in your application's bootstrap/app.php
to register a closure that should be executed when an exception of a given type needs to be reported. Laravel will determine what type of exception the closure reports by examining the type-hint of the closure:
->withExceptions(function (Exceptions $exceptions) {
$exceptions->report(function (InvalidOrderException $e) {
// ...
});
})
When you register a custom exception reporting callback using the report
method, Laravel will still log the exception using the default logging configuration for the application. If you wish to stop the propagation of the exception to the default logging stack, you may use the stop
method when defining your reporting callback or return false
from the callback:
->withExceptions(function (Exceptions $exceptions) {
$exceptions->report(function (InvalidOrderException $e) {
// ...
})->stop();
$exceptions->report(function (InvalidOrderException $e) {
return false;
});
})
If you would like to ensure that a single instance of an exception is only ever reported once, you may invoke the dontReportDuplicates
exception method in your application's bootstrap/app.php
file:
->withExceptions(function (Exceptions $exceptions) {
$exceptions->dontReportDuplicates();
})
You may publish Laravel's default error page templates using the vendor:publish
Artisan command. Once the templates have been published, you may customize them to your liking:
php artisan vendor:publish --tag=laravel-errors
Fallback HTTP Error Pages
You may also define a "fallback" error page for a given series of HTTP status codes. This page will be rendered if there is not a corresponding page for the specific HTTP status code that occurred. To accomplish this, define a 4xx.blade.php
template and a 5xx.blade.php
template in your application's resources/views/errors
directory.
Conclusion
That’s a concise overview of how to manage exceptions in Laravel. For further exploration, you can refer to the official Laravel documentation here: https://laravel.com/docs/11.x/errors
I trust you find this article beneficial. If you did, please consider giving it a like. Thank you!