douya2006
douya2006
2017-05-30 01:49

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 douyingmou1389 4年前

    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.

    点赞 评论 复制链接分享