mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 06:15:10 +08:00
refactor(exception): 重构异常处理逻辑
- 移除 CommonHandler 中的 $request 属性 - 使用 make 函数创建 RequestInterface 实例 - 优化异常日志输出格式 - 调整请求数据和查询字符串的获取方式 Signed-off-by: 李东云 <dongyu.li@luxcreo.ai>
This commit is contained in:
@@ -42,6 +42,7 @@ use Teapot\StatusCode\RFC\RFC7231;
|
|||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
use function Hyperf\Config\config;
|
use function Hyperf\Config\config;
|
||||||
|
use function Hyperf\Support\make;
|
||||||
use function Hyperf\Translation\__;
|
use function Hyperf\Translation\__;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,9 +55,6 @@ use function Hyperf\Translation\__;
|
|||||||
*/
|
*/
|
||||||
class CommonHandler extends ExceptionHandler
|
class CommonHandler extends ExceptionHandler
|
||||||
{
|
{
|
||||||
#[Inject(required: false)]
|
|
||||||
private ?RequestInterface $request;
|
|
||||||
|
|
||||||
#[Inject]
|
#[Inject]
|
||||||
private StdoutLogger $logger;
|
private StdoutLogger $logger;
|
||||||
|
|
||||||
@@ -70,18 +68,23 @@ class CommonHandler extends ExceptionHandler
|
|||||||
{
|
{
|
||||||
// 阻止异常冒泡
|
// 阻止异常冒泡
|
||||||
$this->stopPropagation();
|
$this->stopPropagation();
|
||||||
|
if ($throwable instanceof BadRequestHttpException) {
|
||||||
|
$request = $throwable->getRequest();
|
||||||
|
} else {
|
||||||
|
$request = make(RequestInterface::class);
|
||||||
|
}
|
||||||
$restful = $this->apiStyleService->get();
|
$restful = $this->apiStyleService->get();
|
||||||
// $restful = config('common.response.restful');
|
// $restful = config('common.response.restful');
|
||||||
$code_name = config('common.response.code_name');
|
$code_name = config('common.response.code_name');
|
||||||
$message_name = config('common.response.message_name');
|
$message_name = config('common.response.message_name');
|
||||||
$is_testing = config('app_status') === true;
|
$is_testing = config('app_status') === true;
|
||||||
$this->request?->url();
|
$request?->url();
|
||||||
|
|
||||||
$is_debug = $is_testing;
|
$is_debug = $is_testing;
|
||||||
$error_type = $throwable::class;
|
$error_type = $throwable::class;
|
||||||
$request_time = Carbon::now()->toDateTimeString();
|
$request_time = Carbon::now()->toDateTimeString();
|
||||||
$request_data = Json::encode($this->request?->getParsedBody());
|
$request_data = $request?->getBody()->getContents();
|
||||||
$request_headers = Json::encode($this->request?->getHeaders());
|
$request_headers = Json::encode($request?->getHeaders());
|
||||||
// 901 程序语法错误
|
// 901 程序语法错误
|
||||||
// 902 SQL 语法错误
|
// 902 SQL 语法错误
|
||||||
if ($throwable instanceof QueryException) {
|
if ($throwable instanceof QueryException) {
|
||||||
@@ -125,7 +128,7 @@ class CommonHandler extends ExceptionHandler
|
|||||||
$data = [
|
$data = [
|
||||||
$code_name => $code,
|
$code_name => $code,
|
||||||
$message_name => CommonErrorCode::getMessage($code, ['methods' => join(', ', $allow_method)]),
|
$message_name => CommonErrorCode::getMessage($code, ['methods' => join(', ', $allow_method)]),
|
||||||
'currentMethod' => $this->request?->getMethod(),
|
'currentMethod' => $request?->getMethod(),
|
||||||
'allowedMethod' => $allow_method,
|
'allowedMethod' => $allow_method,
|
||||||
];
|
];
|
||||||
$status_code = RFC7231::METHOD_NOT_ALLOWED;
|
$status_code = RFC7231::METHOD_NOT_ALLOWED;
|
||||||
@@ -257,7 +260,7 @@ class CommonHandler extends ExceptionHandler
|
|||||||
if ($is_debug && $is_testing) {
|
if ($is_debug && $is_testing) {
|
||||||
$data += ['errorType' => $error_type, 'errorTrack' => $throwable->getTrace()];
|
$data += ['errorType' => $error_type, 'errorTrack' => $throwable->getTrace()];
|
||||||
}
|
}
|
||||||
$cookies = json_encode($this->request->getCookieParams(), JSON_UNESCAPED_UNICODE);
|
$cookies = json_encode($request->getCookieParams(), JSON_UNESCAPED_UNICODE);
|
||||||
$this->logger->error(
|
$this->logger->error(
|
||||||
<<<ERROR_LOG
|
<<<ERROR_LOG
|
||||||
TYPE: {$error_type}
|
TYPE: {$error_type}
|
||||||
@@ -273,13 +276,13 @@ REQUEST_COOKIES:
|
|||||||
{$cookies}
|
{$cookies}
|
||||||
-------------------------------
|
-------------------------------
|
||||||
REQUEST_METHOD:
|
REQUEST_METHOD:
|
||||||
{$this->request?->getMethod()}
|
{$request?->getMethod()}
|
||||||
-------------------------------
|
-------------------------------
|
||||||
REQUEST_URL:
|
REQUEST_URL:
|
||||||
{$this->request?->getUri()}
|
{$request?->getUri()}
|
||||||
-------------------------------
|
-------------------------------
|
||||||
REQUEST_QUERY:
|
REQUEST_QUERY:
|
||||||
{$this->request?->getQueryString()}
|
{$request?->getUri()->getQuery()}
|
||||||
-------------------------------
|
-------------------------------
|
||||||
REQUEST_DATA:
|
REQUEST_DATA:
|
||||||
{$request_data}
|
{$request_data}
|
||||||
|
|||||||
Reference in New Issue
Block a user