Skip to main content

Overview

When you create a new Laravel project, error and exception handling is already configured. You customize it through the withExceptions method in bootstrap/app.php:
The $exceptions object passed to the closure is an instance of Illuminate\Foundation\Configuration\Exceptions and manages all exception handling for your application.

Debug mode

The debug option in config/app.php controls how much error detail is shown to users. It reads from the APP_DEBUG environment variable:
Always set APP_DEBUG=false in production. Leaving it true risks exposing sensitive configuration values to your users.

Reporting exceptions

Reporting means logging exceptions or sending them to an external service like Sentry or Flare. By default, exceptions are logged according to your config/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:
The default logging still runs after the callback. To stop propagation, call stop() or return false:

The report helper

Report an exception without interrupting the current request:
The report helper is useful for background jobs and non-critical operations where you want to record the error without returning an error page to the user.

Deduplicating reported exceptions

Calling report 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 a context() 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 the level method:

Throttling exception reports

Limit how many exceptions are reported when a large number occur in a short period:
Rate-limit by time using 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

Override built-in exceptions such as 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 the Accept request header. Customize this logic with shouldRenderJsonWhen:

Customizing the full response

Use respond to modify the response after it has been rendered:

Custom exception classes

Create exception classes in app/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:
These helpers are useful for authorization checks in controllers and middleware, often alongside gates and policies.

Controlling exception reporting globally

Ignoring exceptions

Prevent specific exception types from being reported with dontReport:
Ignore exceptions conditionally with dontReportWhen:
Laravel automatically ignores certain exceptions by default, including 404 errors, 419 CSRF token mismatches, and 403 origin mismatches.

Re-enabling ignored exceptions

Use stopIgnoring 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 in resources/views/errors/:
Access error details through the $exception variable:

Publish the default templates

Customize Laravel’s built-in error pages by publishing them first:

Fallback error pages

Create 4xx.blade.php and 5xx.blade.php as fallbacks when no page exists for a specific status code:
Fallback pages do not apply to 404, 500, and 503 responses — Laravel has dedicated internal pages for those. Create individual files such as 404.blade.php to customize them.

Summary

  • Create resources/views/errors/404.blade.php (and so on) to override error pages automatically
  • Access error details through the $exception variable in the view
  • Run php artisan vendor:publish --tag=laravel-errors to get the default templates
  • Use 4xx.blade.php and 5xx.blade.php as fallbacks for unhandled status codes
  • Set APP_DEBUG=false so 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
Last modified on March 29, 2026