From 3a1f912520999ec947295efed564448a85ad1039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=9C=E4=BA=91?= Date: Tue, 12 Aug 2025 09:54:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor(exception):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 CommonHandler 中的 $request 属性 - 使用 make 函数创建 RequestInterface 实例 - 优化异常日志输出格式 - 调整请求数据和查询字符串的获取方式 Signed-off-by: 李东云 --- src/Exceptions/Handler/CommonHandler.php | 75 ++++++++++++------------ 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/src/Exceptions/Handler/CommonHandler.php b/src/Exceptions/Handler/CommonHandler.php index 1e26c77..f683f2f 100644 --- a/src/Exceptions/Handler/CommonHandler.php +++ b/src/Exceptions/Handler/CommonHandler.php @@ -42,6 +42,7 @@ use Teapot\StatusCode\RFC\RFC7231; use Throwable; use function Hyperf\Config\config; +use function Hyperf\Support\make; use function Hyperf\Translation\__; /** @@ -54,9 +55,6 @@ use function Hyperf\Translation\__; */ class CommonHandler extends ExceptionHandler { - #[Inject(required: false)] - private ?RequestInterface $request; - #[Inject] private StdoutLogger $logger; @@ -70,18 +68,23 @@ class CommonHandler extends ExceptionHandler { // 阻止异常冒泡 $this->stopPropagation(); + if ($throwable instanceof BadRequestHttpException) { + $request = $throwable->getRequest(); + } else { + $request = make(RequestInterface::class); + } $restful = $this->apiStyleService->get(); // $restful = config('common.response.restful'); $code_name = config('common.response.code_name'); $message_name = config('common.response.message_name'); $is_testing = config('app_status') === true; - $this->request?->url(); + $request?->url(); $is_debug = $is_testing; $error_type = $throwable::class; $request_time = Carbon::now()->toDateTimeString(); - $request_data = Json::encode($this->request?->getParsedBody()); - $request_headers = Json::encode($this->request?->getHeaders()); + $request_data = $request?->getBody()->getContents(); + $request_headers = Json::encode($request?->getHeaders()); // 901 程序语法错误 // 902 SQL 语法错误 if ($throwable instanceof QueryException) { @@ -125,7 +128,7 @@ class CommonHandler extends ExceptionHandler $data = [ $code_name => $code, $message_name => CommonErrorCode::getMessage($code, ['methods' => join(', ', $allow_method)]), - 'currentMethod' => $this->request?->getMethod(), + 'currentMethod' => $request?->getMethod(), 'allowedMethod' => $allow_method, ]; $status_code = RFC7231::METHOD_NOT_ALLOWED; @@ -257,37 +260,37 @@ class CommonHandler extends ExceptionHandler if ($is_debug && $is_testing) { $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( <<getMessage()} -------------------------------- -REQUEST_TIME: {$request_time} -------------------------------- -REQUEST_HEADERS: -{$request_headers} -------------------------------- -REQUEST_COOKIES: -{$cookies} -------------------------------- -REQUEST_METHOD: -{$this->request?->getMethod()} -------------------------------- -REQUEST_URL: -{$this->request?->getUri()} -------------------------------- -REQUEST_QUERY: -{$this->request?->getQueryString()} -------------------------------- -REQUEST_DATA: -{$request_data} -------------------------------- -TRACE: -{$throwable->getTraceAsString()} -=============================== -ERROR_LOG, + TYPE: {$error_type} + [$data[$code_name]] $data[$message_name] + {$throwable->getMessage()} + ------------------------------- + REQUEST_TIME: {$request_time} + ------------------------------- + REQUEST_HEADERS: + {$request_headers} + ------------------------------- + REQUEST_COOKIES: + {$cookies} + ------------------------------- + REQUEST_METHOD: + {$request?->getMethod()} + ------------------------------- + REQUEST_URL: + {$request?->getUri()} + ------------------------------- + REQUEST_QUERY: + {$request?->getUri()->getQuery()} + ------------------------------- + REQUEST_DATA: + {$request_data} + ------------------------------- + TRACE: + {$throwable->getTraceAsString()} + =============================== + ERROR_LOG, ); $data = Json::encode($data); if ($restful === ApiStyleService::RESTFUL) {