Laravel处理自定义异常

时间:2016-03-18 10:42:28

标签: php exception-handling laravel-5

我想处理自定义异常(PDOException,...)

在App / Exceptions / Handler.php中,我添加了以下功能:

public function report(Exception $e)
{
    if ($e instanceof \PDOException) {
        //$this->renderHttpException($e);
        //Doesn't work
    }
    else 
    {
        parent::report($e);
    }
}

public function render($request, Exception $e)
{
        if($this->isHttpException($e))
        {
            return $this->renderHttpException($e);          
        }
        else 
        {
            return parent::render($request, $e);
        }
}

protected function renderHttpException(HttpException $e)
{
    if (view()->exists('errors.'.$e->getStatusCode()))
    {
        return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode());
    }
    else
    {
        //comes later
        return null;
    }
}

如果我有PDOException,我无法处理它,因为PDOException看起来不像默认的Laravel异常。

我知道它不起作用,但我不知道如何使其发挥作用。

这是Laravel(5.1)错误:

Argument 1 passed to App\Exceptions\Handler::renderHttpException() must be 
an instance of 
Symfony\Component\HttpKernel\Exception\HttpException, instance of 
PDOException given, called in /home/****/Workspace/****/app/Exceptions/Handler.php 
on line 37 and defined

函数源来自 mattstauffer.co

0 个答案:

没有答案