Skip to content

Instantly share code, notes, and snippets.

@greabock
Created November 3, 2015 16:55
Show Gist options
  • Save greabock/7c314adfb571b7236b88 to your computer and use it in GitHub Desktop.
Save greabock/7c314adfb571b7236b88 to your computer and use it in GitHub Desktop.
<?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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment