Error Handling | Laravel - Error Handling




 laravel error handling

What is Error handling in Laravel?

  • Error handling refers to the anticipation, detection, and resolution of programming, application, and communications errors.
  • Specialized programs, called error handlers, are available for some applications.
  • The best programs of this type forestall errors if possible, recover from them when they occur without terminating the application, or (if all else fails) gracefully terminate an affected application and save the error information to a log file.
  • In Laravel all the exceptions are handled by app\Exceptions\Handler class.
  • This class contains two methods - report and render.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

report() method

  • report() method is used to report or log exception.
  • It is also used to send log exceptions to external services like Sentry, Bugsnag etc.
  • By default, the report method simply passes the exception to the base class where the exception is logged. However, you are free to log exceptions however you wish.
  • For example, if you need to report different types of exceptions in different ways, you may use the PHP instanceof comparison operator:
/**
 * Report or log an exception.
 *
 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
 *
 * @param  \Exception  $exception
 * @return void
 */
public function report(Exception $exception)
{
    if ($exception instanceof CustomException) {
        //
    }
    return parent::report($exception);
}

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.
/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception)
{
    if ($exception instanceof CustomException) {
        return response()->view('errors.custom', [], 500);
    }

    return parent::render($request, $exception);
}
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

HTTP Exceptions:

  • Some exceptions describe HTTP error codes like 404, 500 etc.
  • To generate such response anywhere in an application, you can use abort() method as follows.
 abort(404)
  • The abort helper will immediately raise an exception which will be rendered by the exception handler. Optionally, you may provide the response text:
abort(403, 'Unauthorized action.');

Custom Error pages:

  • Laravel makes it very easy for us to use the custom error pages for each separate error codes.
  • For example, if you want 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
Route::get('/error',function(){
   abort(404);
});
  • 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
<!DOCTYPE html>
<html>
       <head>
      <title>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';
         }
         .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">404 Error</div>
         </div>
      </div>
		
   </body>
</html>
  • Step 3 − Visit the following URL to test the event.
  • http://localhost:8000/error
  • Step 4 − After visiting the URL, you will receive the following output −
 laravel

This tutorial is used to learn laravel and also provides guidance on laravel book , laravel books , laravel hosting , laravel programming , laravel server php , laravel server , laravel development server , start laravel server , laravel getting started , laravel programmer , php with laravel for beginners , what is laravel framework in php , up and running with laravel , php framework laravel , getting started with laravel , what is laravel framework , laravel applications , what is laravel in php , laravel hosting , laravel framework , php laravel framework , latest framework in php

Related Searches to Error Handling | Laravel - Error Handling

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×