12.02.2018 Views

laravel_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

22. Laravel – Error Handling<br />

Laravel<br />

In Laravel all the exceptions are handled by app\Exceptions\Handler class. This class<br />

contains two methods — report and render.<br />

report() method<br />

report() method is used to report or log exception. It is also used to send log exceptions<br />

to external services like Sentry, Bugsnag etc.<br />

render() method<br />

render() method is used to render an exception into an HTTP response which will be sent<br />

back to browser.<br />

Beside these two methods, the app\Exceptions\Handler class contains an important<br />

property called “$dontReport”. This property takes an array of exception types that will<br />

not be logged.<br />

HTTP Exceptions<br />

Some exceptions describe HTTP error codes like 404, 500 etc. To generate such response<br />

anywhere in an application, you can use abort() method as follows.<br />

abort(404)<br />

Custom Error pages<br />

Laravel makes it very easy for us to use the custom error pages for each separate error<br />

codes. For example, if you want to design custom page for error code 404, you can create<br />

a view at resources/views/errors/404.blade.php. Same way, if you want to design<br />

error page for error code 500, it should be stored at<br />

resources/views/errors/500.blade.php.<br />

Example<br />

Step 1: Add the following lines in app/Http/routes.php.<br />

app/Http/routes.php<br />

Route::get('/error',function(){<br />

abort(404);<br />

});<br />

111

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!