Skip to content

Instantly share code, notes, and snippets.

@butschster
Forked from greabock/Handler.php
Created November 4, 2015 11:05
Show Gist options
  • Save butschster/56a950a3f6e1ab7e1032 to your computer and use it in GitHub Desktop.
Save butschster/56a950a3f6e1ab7e1032 to your computer and use it in GitHub Desktop.
<?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);
}
}
}
}
@butschster
Copy link
Author

Примеры использования

$exceptionHandler->addHandlerReporter(function ($e) {
        return Request::ajax() or ( $e instanceof APIException );
}, Handler::class);
$exceptionHandler->addHandlerReporter(ModelNotFoundException::class, function(Request $request, ModelNotFoundException $e) {
    $model = $e->getModel();
    if (method_exists($model, 'getNotFoundMessage')) {
        $message = Callback::invoke($model . '@' . 'getNotFoundMessage');
    } else {
        $message = $e->getMessage();
    }

    return redirect()
            ->back()
            ->withErrors($message);
});

$exceptionHandler->addHandlerReporter(ValidationException::class, function(Request $request, ValidationException $e) {
    $errors = $e->getValidator()->errors()->getMessages();

    return redirect()
            ->back()
            ->withInput($request->input())
            ->withErrors($errors);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment