Skip to content

Instantly share code, notes, and snippets.

@greabock
Created November 3, 2015 16:55
Show Gist options
  • Select an option

  • Save greabock/7c314adfb571b7236b88 to your computer and use it in GitHub Desktop.

Select an option

Save greabock/7c314adfb571b7236b88 to your computer and use it in GitHub Desktop.

Revisions

  1. greabock created this gist Nov 3, 2015.
    105 changes: 105 additions & 0 deletions Handler.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    <?php namespace App\Core\Handlers\Exceptions;

    use Exception;
    use Illuminate\Foundation\Exceptions\Handler;


    class ExceptionHandler extends Handler
    {


    /**
    * @var \Illuminate\Contracts\Debug\ExceptionHandler
    */
    protected $subHandler;

    /**
    * @var array
    */
    protected $subHandlers = [];

    /**
    * A list of the exception types that should not be reported.
    *
    * @var array
    */
    protected $dontReport = [
    'Symfony\Component\HttpKernel\Exception\HttpException'
    ];

    /**
    * Report or log an exception.
    * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
    *
    * @param \Exception $e
    * @return void
    */
    public function report(Exception $e)
    {
    if ($this->subHandlerReport($e) !== false) {
    parent::report($e);
    }
    }

    /**
    * Render an exception into an HTTP response.
    *
    * @param \Illuminate\Http\Request $request
    * @param \Exception $e
    * @return \Illuminate\Http\Response
    */
    public function render($request, Exception $e)
    {
    if ($this->subHandler) {

    return $this->subHandler->render($request, $e);
    }

    return parent::render($request, $e);
    }

    /**
    * @param $exception
    */
    public function dontReport($exception)
    {
    $this->dontReport[] = $exception;
    }

    /**
    * @param $exception
    * @param $handler
    */
    public function addSubHandler($exception, $handler)
    {
    $this->subHandlers[$exception] = $handler;
    }

    /**
    * @param string | \Exception $e
    * @return bool
    */
    public function handlerRegistered($e)
    {
    if (is_object($e) && $e instanceof Exception) {

    return isset($this->subHandlers[get_class($e)]);
    }

    return isset($this->subHandlers[$e]);
    }

    /**
    * @param \Exception $e
    * @return boolean | void
    */
    public function subHandlerReport(Exception $e)
    {
    if ($this->handlerRegistered($e)) {

    $this->subHandler = app($this->subHandlers[get_class($e)]);

    return $this->subHandler->report($e);
    }
    }
    }