From 0247dd415c2a62c21b5a6fbb15ab1e6ce3d1a316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=9C=E4=BA=91?= Date: Tue, 25 Apr 2023 17:18:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(handler):=20=E8=A1=A5=E5=85=A8=E4=BA=86?= =?UTF-8?q?=E5=90=84=E7=A7=8D=E9=94=99=E8=AF=AF=E7=9A=84=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Exceptions/Handler/CommonHandler.php | 84 +++++++++++++++++++----- 1 file changed, 68 insertions(+), 16 deletions(-) diff --git a/src/Exceptions/Handler/CommonHandler.php b/src/Exceptions/Handler/CommonHandler.php index 0b88997..f12ced0 100644 --- a/src/Exceptions/Handler/CommonHandler.php +++ b/src/Exceptions/Handler/CommonHandler.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Singularity\HDK\Core\Exceptions\Handler; +use Carbon\Carbon; use Hyperf\Database\Exception\QueryException; use Hyperf\Database\Model\ModelNotFoundException; use Hyperf\Di\Annotation\Inject; @@ -31,7 +32,6 @@ use RedisException; use Singularity\HDK\Core\Constants\CommonErrorCode; use Singularity\HDK\Core\Exceptions\ValidateException; use Symfony\Component\Mailer\Exception\TransportException; -use Teapot\StatusCode\All; use Teapot\StatusCode\RFC\RFC4918; use Teapot\StatusCode\RFC\RFC7231; use Throwable; @@ -57,6 +57,7 @@ class CommonHandler extends ExceptionHandler */ #[Inject] private StdoutLogger $logger; + /** * {@inheritDoc} */ @@ -69,9 +70,12 @@ class CommonHandler extends ExceptionHandler $message_name = config('common.response.message_name'); $is_testing = config('app_status') === true; $this->request?->url(); - $is_debug = $this->request?->hasHeader('Postman-Token') || str_starts_with($this->request?->header('User-Agent', ''), 'apifox'); + $is_debug = $this->request?->hasHeader('Postman-Token') || str_starts_with( + $this->request?->header('User-Agent', ''), + 'apifox' + ); $error_type = $throwable::class; - $request_time = date('Y-m-d H:i:s'); + $request_time = Carbon::now()->toDateTimeString(); $request_data = Json::encode($this->request?->getParsedBody()); $request_headers = Json::encode($this->request?->getHeaders()); // 901 程序语法错误 @@ -81,7 +85,10 @@ class CommonHandler extends ExceptionHandler '42S22' => CommonErrorCode::PROGRAM_SQL_COLUMN_NOT_FOUND, default => CommonErrorCode::PROGRAM_SQL_ERROR, }; - $data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($is_testing ? $code : CommonErrorCode::PROGRAM_SQL_ERROR)]; + $data = [ + $code_name => $code, + $message_name => CommonErrorCode::getMessage($is_testing ? $code : CommonErrorCode::PROGRAM_SQL_ERROR), + ]; if ($is_testing) { $data['details'] = ['sql' => $throwable->getSql(), 'error' => $throwable->getMessage()]; } @@ -91,27 +98,48 @@ class CommonHandler extends ExceptionHandler $message = explode(': ', $throwable->getMessage()); $allow_method = explode(', ', $message[1]); $code = CommonErrorCode::REQUEST_METHOD_ERROR; - $data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($code, ['methods' => join(', ', $allow_method)]), 'currentMethod' => $this->request?->getMethod(), 'allowedMethod' => $allow_method]; + $data = [ + $code_name => $code, + $message_name => CommonErrorCode::getMessage($code, ['methods' => join(', ', $allow_method)]), + 'currentMethod' => $this->request?->getMethod(), + 'allowedMethod' => $allow_method, + ]; + $status_code = RFC7231::METHOD_NOT_ALLOWED; } // 验证失败 if ($throwable instanceof BadRequestHttpException) { + $data = [ + $code_name => CommonErrorCode::REQUEST_PARAMS_ERROR, + $message_name => $is_testing ? $throwable->getMessage() : CommonErrorCode::getMessage( + CommonErrorCode::SERVER_ERROR + ), + ]; $status_code = RFC4918::UNPROCESSABLE_ENTITY; - $data = [$code_name => CommonErrorCode::REQUEST_PARAMS_ERROR, $message_name => $is_testing ? $throwable->getMessage() : CommonErrorCode::getMessage(CommonErrorCode::SERVER_ERROR)]; } if ($throwable instanceof ValidationException) { - $status_code = RFC4918::UNPROCESSABLE_ENTITY; $data = $throwable->validator->errors()->first(); if (is_numeric($data)) { - $code = (int) $data; + $code = (int)$data; $data = CommonErrorCode::getMessage($code); } $data = [$code_name => $code ?? CommonErrorCode::REQUEST_PARAMS_ERROR, $message_name => $data]; + $status_code = RFC4918::UNPROCESSABLE_ENTITY; } if ($throwable instanceof ValidateException) { $status_code = RFC4918::UNPROCESSABLE_ENTITY; $code = $throwable->getCode(); $message = $throwable->getMessage(); - $data = [$code_name => $code, $message_name => empty($message) ? CommonErrorCode::getMessage($code, ['param' => $throwable->getFieldName()]) : $message]; + $data = [ + $code_name => $code, + $message_name => empty($message) + ? CommonErrorCode::getMessage( + $code, + [ + 'param' => $throwable->getFieldName(), + ] + ) + : $message, + ]; if ($is_debug) { $data['currentValue'] = $throwable->getCurrentValue(); $data['availableValue'] = $throwable->getAvailableValue(); @@ -126,7 +154,8 @@ class CommonHandler extends ExceptionHandler // 模型不存在 if ($throwable instanceof ModelNotFoundException) { $code = empty($throwable->getCode()) ? CommonErrorCode::MODEL_NOT_FOUND : $throwable->getCode(); - $message = empty($throwable->getCode()) ? CommonErrorCode::getMessage($code, ['resource' => '资源']) : $throwable->getMessage(); + $message = empty($throwable->getCode()) ? CommonErrorCode::getMessage($code, ['resource' => '资源'] + ) : $throwable->getMessage(); $data = [$code_name => $code, $message_name => $message]; $status_code = RFC7231::NOT_FOUND; } @@ -137,7 +166,12 @@ class CommonHandler extends ExceptionHandler if ($throwable->getMessage() === 'Connection refused') { $code = CommonErrorCode::SERVER_CACHE_REDIS_REFUSED_ERROR; } - $data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($is_testing ? $code : CommonErrorCode::SERVER_CACHE_REDIS_ERROR)]; + $data = [ + $code_name => $code, + $message_name => CommonErrorCode::getMessage( + $is_testing ? $code : CommonErrorCode::SERVER_CACHE_REDIS_ERROR + ), + ]; } // 306 消息异常 // 30601 邮箱发件异常 @@ -146,14 +180,29 @@ class CommonHandler extends ExceptionHandler if (strpos($throwable->getMessage(), '500 Error: bad syntax')) { $code = CommonErrorCode::SERVER_MESSAGE_EMAIL_NOT_FOUND; } - $data = [$code_name => $code, $message_name => CommonErrorCode::getMessage($is_testing ? $code : CommonErrorCode::SERVER_MESSAGE_EMAIL_ERROR)]; + $data = [ + $code_name => $code, + $message_name => CommonErrorCode::getMessage( + $is_testing ? $code : CommonErrorCode::SERVER_MESSAGE_EMAIL_ERROR + ), + ]; } if (empty($data)) { // 其他情况 - $data = [$code_name => $is_testing ? $throwable->getCode() == 0 ? CommonErrorCode::SERVER_ERROR : $throwable->getCode() : CommonErrorCode::SERVER_ERROR, $message_name => $is_testing ? $throwable->getMessage() : __(CommonErrorCode::getMessage(CommonErrorCode::SERVER_ERROR))]; + $data = [ + $code_name => $is_testing ? $throwable->getCode( + ) == 0 ? CommonErrorCode::SERVER_ERROR : $throwable->getCode() : CommonErrorCode::SERVER_ERROR, + $message_name => $is_testing ? $throwable->getMessage() : __( + CommonErrorCode::getMessage(CommonErrorCode::SERVER_ERROR) + ), + ]; // 其他错误 if ($throwable instanceof HttpException) { - $data = [$code_name => $throwable->getCode() ?: $throwable->getStatusCode(), $message_name => $throwable->getMessage()]; + $data = [ + $code_name => $throwable->getCode() ?: $throwable->getStatusCode(), + $message_name => $throwable->getMessage(), + ]; + $status_code = $throwable->getStatusCode(); } } $response = $response->withHeader(Header::CONTENT_TYPE, 'application/json; charset=utf-8'); @@ -164,7 +213,7 @@ class CommonHandler extends ExceptionHandler $this->logger->error( <<getMessage()} ------------------------------- REQUEST_TIME: {$request_time} @@ -195,10 +244,13 @@ ERROR_LOG ); $data = Json::encode($data); if ($restful) { - $response = $response->withStatus($status_code ?? $throwable->status ?? $throwable->statusCode ?? RFC7231::INTERNAL_SERVER_ERROR); + $response = $response->withStatus( + $status_code ?? $throwable->status ?? $throwable->statusCode ?? RFC7231::INTERNAL_SERVER_ERROR + ); } return $response->withBody(new SwooleStream($data)); } + /** * 判断该异常处理器是否要对该异常进行处理. */