douya2006 2017-05-30 01:49
浏览 218
已采纳

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

I am trying to ensure that exceptions thrown from database connection issues are handled globally.

I have added the following in my app\Exceptions\Handler.php in the render() method, however none of the exceptions are being caught:

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

This was also added to my app.php:

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

For the life of me I cant get this to work.

Any help/advice would be greatly appreciated

Thanks

  • 写回答

1条回答 默认 最新

  • douyingmou1389 2017-05-30 02:00
    关注

    Change the below line from your app.php

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

    to:

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

    EDIT

    The reason why the interface is required in the above exception handler binding is, when an exception is thrown while processing a Route, the below function is invoked to process that exception.

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

    The if condition checks whether or not the container has a binding for the ExceptionHandler class. And if there is a binding, then the exception will be passed on to that exception handler class to be further handled. If there is no binding declared, then the exception will be re-thrown. Here the binding is checked for Illuminate\Contracts\Debug\ExceptionHandler. That's why when you directly bind using App\Exceptions\Handler::class, the exception is not caught by your exception handler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 问题重发,R语言:代码运行过程中出现如下警告,请求如何解决!
  • ¥15 苍穹外卖拦截器token为null
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响