如何在Laravel 5中处理PDOException


How to handle PDOException in Laravel 5

我已将Exceptions/Handler.php修改为:

    <?php namespace App'Exceptions;
use Exception;
use Illuminate'Foundation'Exceptions'Handler as ExceptionHandler;
use Illuminate'Database'Eloquent'ModelNotFoundException as ModelNotFoundException;
use Symfony'Component'HttpKernel'Exception'NotFoundHttpException;
class Handler extends ExceptionHandler {
    /**
     * 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)
    {
        return 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($e instanceof NotFoundHttpException)
        {
            return response()->view('errors/404');
        }
        elseif ($e instanceof ErrorException) {
            return response()->view('errors/404');
        }
        elseif ($e instanceof ModelNotFoundException) {
            return response()->view('error_log(message)ors/404');
        }
        elseif($e instanceof PDOException)
        {
            return Redirect::to('install.php');
        }
        elseif($e instanceof QueryException)
        {
            return Redirect::to('install.php');
        }
        else return response()->view('errors/error');

    }

每当应用程序出现PDOException时,我都试图重定向到安装,但我只是得到了一般的错误视图(errors/error-来自上面代码的最后一行)。

有没有一种方法可以特别计算PDOException错误?

我想我已经找到了解决方案:

只需放入

use PDOException; 

include使用PDOException;

public function render($request, Exception $e)
    {
        if($e instanceof PDOException)
        {
            return response()->view('errors/404');
        }

异常/处理程序中它对我有效

这就是我在Laravel 5.3中的工作原理(位于/app/Exceptions/中的Handler.php文件的摘录):

public function report(Exception $e)
{
    // Both this, and below, are needed
    if($e instanceof 'PDOException) {
        return response()->view('errors/404');
    }
    return parent::report($e);
}
public function render($request, Exception $e)
{
    if($e instanceof 'PDOException) {
        // TODO send me an email!
        Session::flash('messageclass', 'danger');
        Session::flash('message', trans('general.pdoexception'));
        return redirect()->back();
    }
    return parent::render($request, $e);
}