Laravel 8 Tutorial - Laravel Error Handling

Published January 29, 2022

One of the advantages of utilizing Laravel 8 is that error and exception handling is pre-configured. An exception handler class in the App\Exceptions\Handler is often responsible for rendering all application exceptions to the user. In this chapter, we'll take a look at how Laravel handles errors and exceptions. The following topics are covered in this chapter:

  • Error display Configuration

  • The Exception Handling Process

  • How to ignore Exceptions

  • How to Render Exceptions

  • The HTTP Exceptions

  • The Customized HTTP error pages

 

1. Configuration

The config/app.php has a debug option that plays an essential role in determining the error information that the application will display to the user. By default, this option has been configured to display the APP_DEBUG environment value that is stored in the.env file.

Most developers make mistakes throughout the configuring process. They usually neglect to check that the APP DEBUG environment setting is set to false, not true. If this is true, the developer is continually at risk of revealing crucial configuration data to application users.

 

2. The Exception Handling Process

In Laravel 8, all the exceptions are handled by the App\Exceptions\Handler class.

In this file, there is a class that contains the register method, and it is where you can register the custom exception and callbacks rendering. Typically the exceptions are logged and rendered depending on your configuration.

 

3. How to ignore Exceptions

There are a few exceptions that you may reasonably ignore while creating a Laravel application. The $dontReport parameter in the exception handler of Laravel 8 enables ignoring the exception by default, which is generally allocated an empty array. You only need to provide the class name on this property if you want to ignore members of that class.

 

4. How to Render Exceptions

In Laravel 8, by default, the exception handler transforms Laravel exceptions into HTTP responses. You may, however, create your own rendering closure for a certain sort of exception.

You can also utilise your exception handler's renderable function. To throw an exception, ensure the renderable method produces an instance of the Illuminate\Http\Response, which the response helper normally creates.

 

5. The HTTP Exceptions

Several of the exceptions are used to demonstrate the HTTP error codes obtained from the server. The "page not found" issue is an excellent example of this kind of problem (404). You basically have to use the abort helper to retrieve responses from all around the program

 

Abort(404);

 

6. The customised HTTP error pages

Customised error messages for numerous HTTP status codes can be displayed prominently in Laravel 8. Laravel 8 has a resources/view/errors/404.blade.php view template, enabling you to customise the 404 HTTP error page. This view is then shown whenever the program generates a 404 error. Using the $exception->getMessage() function, you can then show the exception in your application. For example

<h3> { $exception->getMessage() }</h3>

 

Conclusion

That’s all about Error Handling in Laravel 8

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

691 Views