Laravel - Error Handling
Laravel - Error Handling
Section titled “Laravel - Error Handling”Introduction
Section titled “Introduction”When you start a new Laravel project, error and exception handling is already configured for you. The App\Exceptions\Handler class is where all exceptions triggered by your application are logged and then rendered back to the user. We will dive into this class throughout this documentation.
The report() Method
Section titled “The report() Method”The report() method of the exception handler is used to log exceptions or send them to an external service like Flare, Bugsnag, or Sentry. By default, the report() method passes the exception to the base class where the exception is logged. However, you are free to log exceptions however you wish.
If you need to report different types of exceptions in different ways, you may use the instanceof comparison operator:
public function report(Throwable $exception){ if ($exception instanceof \App\Exceptions\CustomException) { // Custom reporting logic }
parent::report($exception);}The render() Method
Section titled “The render() Method”The render() method is responsible for converting a given exception into an HTTP response that should be sent back to the browser. By default, the exception is passed to the base class which generates a response for you. However, you are free to check the exception type or return your own custom response.
The $dontReport Property
Section titled “The $dontReport Property”The $dontReport property of the exception handler contains an array of exception types that will not be logged. For example, exceptions resulting from 404 errors, as well as several other types of errors, are not written to your log files by default. You may add other exception types to this array as needed:
protected $dontReport = [ \Illuminate\Auth\AuthenticationException::class, \Illuminate\Database\Eloquent\ModelNotFoundException::class, // Add other exception types here];HTTP Exceptions
Section titled “HTTP Exceptions”Some exceptions describe HTTP error codes from the server like 404 (Not Found), 403 (Forbidden), or 500 (Internal Server Error). To generate such a response from anywhere in your application, you may use the abort helper:
abort(404); // Will trigger a 404 Not Found errorabort(403, 'Unauthorized action.'); // Will trigger a 403 Forbidden error with a messageCustom HTTP Error Pages
Section titled “Custom HTTP Error Pages”Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a resources/views/errors/404.blade.php file. This file will be rendered on all 404 errors generated by your application. The views within this directory should be named to match the HTTP status code they correspond to. The HttpException instance raised by the abort function will be passed to the view as an $exception variable.
Example: Custom 404 Page
Section titled “Example: Custom 404 Page”Step 1: Add a route in routes/web.php that will trigger a 404 error.
routes/web.php
Route::get('/trigger-404', function(){ abort(404);});Step 2: Create a view file at resources/views/errors/404.blade.php and add your custom HTML content.
resources/views/errors/404.blade.php
<!DOCTYPE html><html> <head> <title>Page Not Found (404)</title> <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css"/> <style> html, body { height: 100%; } body { margin: 0; padding: 0; width: 100%; color: #B0BEC5; display: table; font-weight: 100; font-family: 'Lato', sans-serif; } .container { text-align: center; display: table-cell; vertical-align: middle; } .content { text-align: center; display: inline-block; } .title { font-size: 72px; margin-bottom: 40px; } </style> </head> <body> <div class="container"> <div class="content"> <div class="title">Oops! Page Not Found. (404 Error)</div> @isset($exception) {{-- Display message if abort was called with one --}} <p>{{ $exception->getMessage() }}</p> @endisset </div> </div> </body></html>Step 3: Visit the following URL in your browser to test the custom error page.
http://localhost:8000/trigger-404
Step 4: After visiting the URL, you will see your custom 404 error page, displaying ‘Oops! Page Not Found. (404 Error)’.
For more advanced error handling techniques, including how to handle specific exception types differently, refer to the official Laravel documentation on Error Handling.