Overview
When you create a new Laravel project, error and exception handling is already configured. You customize it through thewithExceptions method in bootstrap/app.php:
$exceptions object passed to the closure is an instance of Illuminate\Foundation\Configuration\Exceptions and manages all exception handling for your application.
Debug mode
Thedebug option in config/app.php controls how much error detail is shown to users. It reads from the APP_DEBUG environment variable:
Reporting exceptions
Reporting means logging exceptions or sending them to an external service like Sentry or Flare. By default, exceptions are logged according to yourconfig/logging.php configuration.
Custom report callbacks
Register a closure to handle specific exception types differently. Laravel infers the exception type from the closure’s type-hint:stop() or return false:
The report helper
Report an exception without interrupting the current request:
Deduplicating reported exceptions
Callingreport multiple times with the same exception instance can create duplicate log entries. Use dontReportDuplicates to ensure each instance is reported only once:
Global log context
Add shared context data to every exception log entry. If available, the current user’s ID is included automatically:Exception-level context
Define acontext() method on an exception class to include data specific to that exception:
Changing log levels
Log a specific exception at a particular log level using thelevel method:
Throttling exception reports
Limit how many exceptions are reported when a large number occur in a short period:Limit:
Rendering exceptions
Rendering converts an exception into an HTTP response. Laravel handles this automatically, but you can customize it per exception type.Custom render callbacks
NotFoundHttpException. If the closure returns nothing, Laravel’s default rendering is used:
JSON or HTML auto-detection
Laravel detects whether to render HTML or JSON based on theAccept request header. Customize this logic with shouldRenderJsonWhen:
Customizing the full response
Userespond to modify the response after it has been rendered:
Custom exception classes
Create exception classes inapp/Exceptions/. Define report() and render() methods directly on the class instead of in bootstrap/app.php — Laravel calls them automatically.
1
Generate the exception class
2
Implement report() and render()
You can type-hint dependencies in the
report() method and Laravel’s service container will inject them automatically.The ShouldntReport interface
Implement ShouldntReport on exceptions that should never be reported:
Throwing HTTP errors
The abort helper
Throw an HTTP error response from anywhere in your application:
abort_if and abort_unless
Abort conditionally:
Controlling exception reporting globally
Ignoring exceptions
Prevent specific exception types from being reported withdontReport:
dontReportWhen:
Laravel automatically ignores certain exceptions by default, including 404 errors, 419 CSRF token mismatches, and 403 origin mismatches.
Re-enabling ignored exceptions
UsestopIgnoring to report an exception that Laravel ignores by default:
HTTP error pages
Laravel lets you define custom Blade views for any HTTP status code.Creating custom error views
Place Blade templates named after the status code inresources/views/errors/:
$exception variable:
Publish the default templates
Customize Laravel’s built-in error pages by publishing them first:Fallback error pages
Create4xx.blade.php and 5xx.blade.php as fallbacks when no page exists for a specific status code:
Summary
Exception reporting
Exception reporting
Exception rendering
Exception rendering
HTTP error pages
HTTP error pages
- Create
resources/views/errors/404.blade.php(and so on) to override error pages automatically - Access error details through the
$exceptionvariable in the view - Run
php artisan vendor:publish --tag=laravel-errorsto get the default templates - Use
4xx.blade.phpand5xx.blade.phpas fallbacks for unhandled status codes
Production best practices
Production best practices
- Set
APP_DEBUG=falseso stack traces are never shown to users - Integrate with Sentry or Flare for centralized error tracking
- Use
throttle()to prevent log flooding during error spikes - Return consistent JSON error responses on all API endpoints