Compare commits

..

4 Commits

Author SHA1 Message Date
李东云
b2cf757bf6 chore(release): 1.0.0-beta.14 2025-08-12 09:54:19 +08:00
李东云
3a1f912520 refactor(exception): 重构异常处理逻辑
- 移除 CommonHandler 中的 $request 属性
- 使用 make 函数创建 RequestInterface 实例
- 优化异常日志输出格式
- 调整请求数据和查询字符串的获取方式

Signed-off-by: 李东云 <dongyu.li@luxcreo.ai>
2025-08-12 09:54:05 +08:00
李东云
3ca9955069 chore(release): 1.0.0-beta.13 2025-08-08 17:02:45 +08:00
李东云
15da6e6770 fix(CoreMiddleware): 修复分页头部信息设置错误
- 将 'Total-Pages' 头部信息设置为正确的最后一页数
- 添加 Content-Type 头部信息,设置为 application/json

Signed-off-by: 李东云 <dongyu.li@luxcreo.ai>
2025-08-08 17:02:31 +08:00
5 changed files with 60 additions and 40 deletions

View File

@@ -1,4 +1,18 @@
# 版本更新日志
## [1.0.0-beta.14](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-beta.13...v1.0.0-beta.14) (2025-08-12)
### ♻️ Code Refactoring | 代码重构
* **exception:** 重构异常处理逻辑 ([3a1f912](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/3a1f912520999ec947295efed564448a85ad1039))
## [1.0.0-beta.13](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-beta.12...v1.0.0-beta.13) (2025-08-08)
### 🐛 Bug Fixes | Bug 修复
* **CoreMiddleware:** 修复分页头部信息设置错误 ([15da6e6](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/15da6e67702cdd7d31b55ab58ba9cbbb1a698157))
## [1.0.0-beta.12](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-beta.11...v1.0.0-beta.12) (2025-08-08)

View File

@@ -1 +1 @@
1.0.0-beta.12
1.0.0-beta.14

View File

@@ -136,5 +136,5 @@
"url": "https://mirrors.aliyun.com/composer/"
}
},
"version": "1.0.0-beta.12"
"version": "1.0.0-beta.14"
}

View File

@@ -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(
<<<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:
{$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) {

View File

@@ -77,7 +77,7 @@ class CommonCoreMiddleware extends CoreMiddleware
->withHeader('Per-Page', (string)$response->perPage())
->withHeader('Total', (string)$response->total())
->withHeader('Current-Page', (string)$response->currentPage())
->withHeader('Total-Pages', (string)$response->hasPages());
->withHeader('Total-Pages', (string)$response->lastPage());
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $response->nextPageUrl(), 'next');
$fact_response = $this->utilsService->extendLinkToHeader(
$fact_response,
@@ -90,7 +90,10 @@ class CommonCoreMiddleware extends CoreMiddleware
$response->previousPageUrl(),
'prev'
);
return $fact_response->withBody(new SwooleStream(Json::encode($response->items())));
return $fact_response->withBody(new SwooleStream(Json::encode($response->items())))->withHeader(
Header::CONTENT_TYPE,
'application/json',
);
}
return parent::transferToResponse($response, $request);