douya2006 2017-05-29 17: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-29 18: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部