-
-
Save butschster/56a950a3f6e1ab7e1032 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Exceptions; | |
use Closure; | |
use Exception; | |
use Illuminate\Database\Eloquent\ModelNotFoundException; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
use Illuminate\Foundation\Exceptions\Handler as BaseHandler; | |
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract; | |
class Handler extends BaseHandler | |
{ | |
/** | |
* @var Closure|\Illuminate\Contracts\Debug\ExceptionHandler | |
*/ | |
protected $handler; | |
/** | |
* @var array | |
*/ | |
protected $handlers = []; | |
/** | |
* A list of the exception types that should not be reported. | |
* | |
* @var array | |
*/ | |
protected $dontReport = [ | |
HttpException::class, | |
ModelNotFoundException::class, | |
]; | |
/** | |
* 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->handlerReporter($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 (is_callable($this->handler)) { | |
return call_user_func_array($this->handler, [$request, $e]); | |
} else if ($this->handler instanceof ExceptionHandlerContract) { | |
return $this->handler->render($request, $e); | |
} | |
return parent::render($request, $e); | |
} | |
/** | |
* @param string|Closure $exception | |
* @param $handler | |
*/ | |
public function addHandlerReporter($exception, $handler) | |
{ | |
$this->handlers[] = [$exception, $handler]; | |
} | |
/** | |
* @param string | \Exception $e | |
* | |
* @return bool | |
*/ | |
protected function handlerRegistered($e) | |
{ | |
if (is_object($e) && $e instanceof Exception) { | |
$exceptionClass = get_class($e); | |
} | |
foreach ($this->handlers as $handler) { | |
if (is_callable($handler[0])) { | |
$status = call_user_func($handler[0], $e); | |
if ($status === true) { | |
return $handler[1]; | |
} | |
} else { | |
if ($handler[0] == $exceptionClass) { | |
return $handler[1]; | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* @param Exception $e | |
* | |
* @return boolean | void | |
*/ | |
protected function handlerReporter(Exception $e) | |
{ | |
if (( $handler = $this->handlerRegistered($e) ) !== false) { | |
if (is_callable($handler)) { | |
$this->handler = $handler; | |
return true; | |
} else { | |
$this->handler = app($handler); | |
return $this->handler->report($e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Примеры использования