流明:异常处理程序 - 不捕获 QueryException 或 PDOException

Lumen: Exception Handler - Not catching QueryException or PDOException

我正在努力确保从数据库连接问题抛出的异常得到全局处理。

我在 app\Exceptions\Handler.php 的 render() 方法中添加了以下内容,但是 none 的异常被捕获:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use PDOException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::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)
    {
        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 AuthorizationException) {
            return response()->json((['status' => 403, 'message' => 'Insufficient privileges to perform this action']), 403);
        }

        if ($e instanceof MethodNotAllowedHttpException) {
            return response()->json((['status' => 405, 'message' => 'Method Not Allowed']), 405);
        }

        if ($e instanceof NotFoundHttpException) {
            return response()->json((['status' => 404, 'message' => 'The requested resource was not found']), 404);
        }

        if ($e instanceof QueryException) {
            return response()->json((['id' => 0, 'status_billing' => 'The requested resource was not found']), 500);
        }

        if ($e instanceof PDOException) {
            return response()->json((['id' => 0, 'status_billing' => 'The requested resource was not found']), 500);
        }


        return parent::render($request, $e);
    }
}

这也被添加到我的 app.php:

$app->singleton( App\Exceptions\Handler::class );

为了我的生活,我无法让它工作。

任何 help/advice 将不胜感激

谢谢

更改您 app.php

中的以下行
$app->singleton( App\Exceptions\Handler::class );

至:

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

编辑

上述异常处理程序绑定中需要接口的原因是,当处理路由时抛出异常时,将调用以下函数来处理该异常。

protected function handleException($passable, Exception $e)
    {
        if (! $this->container->bound(ExceptionHandler::class) || ! $passable instanceof Request) {
            throw $e;
        }

        $handler = $this->container->make(ExceptionHandler::class);

        $handler->report($e);

        return $handler->render($passable, $e);
    }

if 条件检查容器是否绑定了 ExceptionHandler class。如果存在绑定,则异常将传递给该异常处理程序 class 以进一步处理。如果没有声明绑定,那么将重新抛出异常。此处检查了 Illuminate\Contracts\Debug\ExceptionHandler 的绑定。这就是为什么当您使用 App\Exceptions\Handler::class 直接绑定时,您的异常处理程序不会捕获异常。