Laravel Error Handling - Laravel

How to handle Errors using Laravle?

Laravel has class called ‘app\Exceptions\Handler’ to handle all the exceptions. This class has two methods as report and render.

report() method

report() method is used to log exception and also used to send log exceptions to external services like Sentry, Bugsnag etc.

render() method

render() method is used to render an exception into an HTTP response, which will be sent back to browser.

Beside these two methods, the app\Exceptions\Handler class contains an important property called “$dontReport”. This property takes an array of exception types that will not be logged.

HTTP Exceptions

Some exceptions describe HTTP error codes like 404, 500 etc.

The abort() method would be used to generate such response anywhere in an application, as follows.

Custom Error pages

Laravel has simple and easy process to use the custom error pages for each separate error codes.

For example, to design custom page for error code 404, you can create a view at resources/views/errors/404.blade.php. Same way, if you want to design error page for error code 500, it should be stored at resources/views/errors/500.blade.php.

Example

Step 1 − Add the following lines in app/Http/routes.php.

app/Http/routes.php

Step 2 − Create a view file called resources/views/errors/404.blade.phpand copy the following code in that file.

resources/views/errors/404.blade.php

Step 3 − Visit the following URL to test the event.

http://localhost:8000/error

Step 4 −We will see the following output

404error

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Laravel Topics