vendredi 18 mars 2016

Laravel handle custom Exceptions

I want to handle custom Exceptions (PDOException, ...)

In App/Exceptions/Handler.php I add the following functions:

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;
    }
}

If I have a PDOException I can't handle it, because the PDOException doesn't look like a default Laravel Exception.

I know that it can't work, but I don't know how to make it work.

Here is the Laravel (5.1) Error:

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

The function source is from mattstauffer.co



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire