LARAVEL

Laravel Error handling


Error handling in Laravel involves managing exceptions and errors that occur during the execution of your application. Laravel provides a robust system for handling errors and exceptions, which includes logging, reporting, and responding to errors gracefully.

 

1. App\Exceptions\Handler:

Laravel's Handler class is where you manage exception handling for your application.

   <?php

   namespace App\Exceptions;

   use Exception;
   use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

   class Handler extends ExceptionHandler
   {
       // Exception handling methods
   }

 

2. Exception Reporting:

The report method in the Handler class is responsible for logging exceptions.

   public function report(Exception $exception)
   {
       if ($exception instanceof CustomException) {
           // Log custom exceptions
       }

       parent::report($exception);
   }

 

3. Exception Rendering:

The render method in the Handler class handles how exceptions are rendered into HTTP responses.

   public function render($request, Exception $exception)
   {
       if ($exception instanceof NotFoundHttpException) {
           return response()->view('errors.404', [], 404);
       }

       return parent::render($request, $exception);
   }

 

4. HTTP Exceptions:

Laravel provides various HTTP exception classes to represent common HTTP errors.

   use Symfony\Component\HttpKernel\Exception\HttpException;

   throw new HttpException(404, 'The resource was not found.');

 

5. Custom Error Pages:

You can create custom error pages for specific HTTP status codes in the resources/views/errors directory.

For example, create 404.blade.php for a custom 404 error page.

 

6. Logging Configuration:

Logging configuration is managed in the config/logging.php file. Here's an example configuration for logging to a single file:

   'channels' => [
       'stack' => [
           'driver' => 'stack',
           'channels' => ['single'],
       ],

       'single' => [
           'driver' => 'single',
           'path' => storage_path('logs/laravel.log'),
           'level' => 'debug',
       ],
   ],

 

7. Logging Levels:

You can specify different logging levels for each channel in the logging configuration.

   'single' => [
       'driver' => 'single',
       'path' => storage_path('logs/laravel.log'),
       'level' => 'debug', // Log level
   ],

 

8. Try-Catch Blocks:

Use PHP's native try-catch blocks to handle exceptions gracefully.

   try {
       // Code that may throw an exception
   } catch (Exception $e) {
       // Handle the exception
   }

 

9. Global Error Handling:

Global error handling logic can be defined in the App\Exceptions\Handler class.

   public function render($request, Exception $exception)
   {
       // Global error handling logic
   }

 

10. Specific Error Handling:

You can handle specific types of errors or exceptions differently based on your application requirements.

   public function render($request, Exception $exception)
   {
       if ($exception instanceof CustomException) {
           // Handle custom exception
       }

       return parent::render($request, $exception);
   }

These examples illustrate how error handling works in Laravel and how you can customize it to suit your application's needs.


LARAVEL