Laravel Error Handling: The Exception Handler

  • App\Exceptions\Handler.
  • Report.
  • Render.

The Report Method

Is used to log exceptions or send them to an external service like Bugsnag or Sentry.

/**
 * 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);
}

Passes the exception to the base class where the exception is logged.

The Render Method

Is responsible for converting a given exception into an HTTP response that should be sent back to the browser.

/**
 * 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);
}

Reportable & Renderable Exceptions

  • Report.
  • Render.
  • <?php namespace App\Exceptions; use Exception; class RenderException extends Exception { /** * Report the exception. * * @return void */ public function report() { // } /** * Render the exception into an HTTP response. * * @param \Illuminate\Http\Request * @return \Illuminate\Http\Response */ public function render($request) { return response(...); } }.

Laravel Error Handling: The Exception Handler — Structure map

Clickable & Draggable!

Laravel Error Handling: The Exception Handler — Related pages: