gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:08:19 25 4
gpt4 key购买 nike

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

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

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

我这辈子都无法让它发挥作用。

任何帮助/建议将不胜感激

谢谢

最佳答案

更改 app.php 中的以下行

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

到:

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

编辑

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

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

关于php - 流明:异常处理程序 - 不捕获 QueryException 或 PDOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44252263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com