mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 05:35:09 +08:00
Compare commits
6 Commits
v1.0.0-bet
...
v1.0.0-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2cf757bf6 | ||
|
|
3a1f912520 | ||
|
|
3ca9955069 | ||
|
|
15da6e6770 | ||
|
|
68451820b0 | ||
|
|
55da02080e |
3
.idea/php.xml
generated
3
.idea/php.xml
generated
@@ -15,9 +15,6 @@
|
||||
<PhpCSFixerConfiguration standards="PSR1;PSR2;Symfony;DoctrineAnnotation;PHP70Migration;PHP71Migration" tool_path="$PROJECT_DIR$/vendor/friendsofphp/php-cs-fixer/php-cs-fixer" />
|
||||
</phpcsfixer_settings>
|
||||
</component>
|
||||
<component name="PhpExternalFormatter">
|
||||
<option name="externalFormatter" value="PHP_CBF" />
|
||||
</component>
|
||||
<component name="PhpIncludePathManager">
|
||||
<include_path>
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/pool" />
|
||||
|
||||
21
CHANGELOG.md
21
CHANGELOG.md
@@ -1,4 +1,25 @@
|
||||
# 版本更新日志
|
||||
## [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)
|
||||
|
||||
|
||||
### ✨ Features | 新功能
|
||||
|
||||
* **exception:** 新增业务错误处理和 API 迁移支持 ([55da020](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/55da02080efa5bd6978b7d0a0e3bbcb1cde41027))
|
||||
|
||||
## [1.0.0-beta.11](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-beta.10...v1.0.0-beta.11) (2025-07-11)
|
||||
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
1.0.0-beta.11
|
||||
1.0.0-beta.14
|
||||
@@ -136,5 +136,5 @@
|
||||
"url": "https://mirrors.aliyun.com/composer/"
|
||||
}
|
||||
},
|
||||
"version": "1.0.0-beta.11"
|
||||
"version": "1.0.0-beta.14"
|
||||
}
|
||||
|
||||
@@ -163,4 +163,11 @@ return [
|
||||
'default' => 'DOC deleted',
|
||||
],
|
||||
],
|
||||
|
||||
// 业务错误
|
||||
'business' => [
|
||||
'default' => 'Business Error',
|
||||
'old_version' => 'This feature is not available in the current version.',
|
||||
'not_implemented' => 'This feature is not implemented yet.',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -172,4 +172,11 @@ return [
|
||||
'default' => '文档已删除',
|
||||
],
|
||||
],
|
||||
|
||||
// 业务错误
|
||||
'business' => [
|
||||
'default' => '业务错误',
|
||||
'old_version' => '当前版本过低,请升级到最新版本',
|
||||
'not_implemented' => '该功能尚未实现',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Singularity\HDK\Core\Constants;
|
||||
|
||||
use Hyperf\Constants\AbstractConstants;
|
||||
use Hyperf\Constants\Annotation\Constants;
|
||||
use Hyperf\Constants\Annotation\Message;
|
||||
|
||||
/**
|
||||
* Singularity\HyperfDevelopmentKit\Utils\Constants\CommonErrorCode@hyperf-development-kit
|
||||
@@ -319,4 +320,15 @@ class CommonErrorCode extends AbstractConstants
|
||||
* @Message("common_error.model.document.default")
|
||||
*/
|
||||
public const DOCUMENT_NOT_EXISTS = 4020201;
|
||||
|
||||
// 500 业务错误
|
||||
|
||||
#[Message('common_error.business.defaul')]
|
||||
public const BUSINESS_ERROR = 500001;
|
||||
// 501 版本问题
|
||||
#[Message('common_error.business.old_version')]
|
||||
public const BUSINESS_FLAT_OLD_VERSION = 501001;
|
||||
|
||||
#[Message('common_error.business.not_implemented')]
|
||||
public const BUSINESS_NOT_IMPLEMENTED = 501002;
|
||||
}
|
||||
|
||||
34
src/Exceptions/ApiMigrated.php
Normal file
34
src/Exceptions/ApiMigrated.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* DomainOrderNotSupportException.php@LuxDesign
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2025/8/1
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Singularity\HDK\Core\Exceptions;
|
||||
|
||||
use Hyperf\HttpMessage\Exception\HttpException;
|
||||
use Singularity\HDK\Core\Constants\CommonErrorCode;
|
||||
use Teapot\StatusCode\RFC\RFC7231;
|
||||
use Throwable;
|
||||
|
||||
final class ApiMigrated extends HttpException
|
||||
{
|
||||
public function __construct(
|
||||
public string $apiPath,
|
||||
int $code = CommonErrorCode::BUSINESS_FLAT_OLD_VERSION,
|
||||
?string $message = null,
|
||||
Throwable $previous = null,
|
||||
) {
|
||||
parent::__construct(RFC7231::SEE_OTHER, $message, $code, $previous);
|
||||
}
|
||||
|
||||
public function getNewPath(): string
|
||||
{
|
||||
return $this->apiPath;
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,8 @@ use Lmc\HttpConstants\Header;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use RedisException;
|
||||
use Singularity\HDK\Core\Constants\CommonErrorCode;
|
||||
use Singularity\HDK\Core\Exceptions\ApiMigrated;
|
||||
use Singularity\HDK\Core\Exceptions\NotImplementedException;
|
||||
use Singularity\HDK\Core\Exceptions\ValidateException;
|
||||
use Singularity\HDK\Core\Service\ApiStyleService;
|
||||
use Symfony\Component\Mailer\Exception\TransportException;
|
||||
@@ -40,6 +42,7 @@ use Teapot\StatusCode\RFC\RFC7231;
|
||||
use Throwable;
|
||||
|
||||
use function Hyperf\Config\config;
|
||||
use function Hyperf\Support\make;
|
||||
use function Hyperf\Translation\__;
|
||||
|
||||
/**
|
||||
@@ -52,9 +55,6 @@ use function Hyperf\Translation\__;
|
||||
*/
|
||||
class CommonHandler extends ExceptionHandler
|
||||
{
|
||||
#[Inject(required: false)]
|
||||
private ?RequestInterface $request;
|
||||
|
||||
#[Inject]
|
||||
private StdoutLogger $logger;
|
||||
|
||||
@@ -68,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) {
|
||||
@@ -123,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;
|
||||
@@ -133,7 +138,7 @@ class CommonHandler extends ExceptionHandler
|
||||
$data = [
|
||||
$code_name => CommonErrorCode::REQUEST_PARAMS_ERROR,
|
||||
$message_name => $is_testing ? $throwable->getMessage() : CommonErrorCode::getMessage(
|
||||
CommonErrorCode::SERVER_ERROR
|
||||
CommonErrorCode::SERVER_ERROR,
|
||||
),
|
||||
];
|
||||
$status_code = RFC4918::UNPROCESSABLE_ENTITY;
|
||||
@@ -158,7 +163,7 @@ class CommonHandler extends ExceptionHandler
|
||||
$code,
|
||||
[
|
||||
'param' => $throwable->getFieldName(),
|
||||
]
|
||||
],
|
||||
)
|
||||
: $message,
|
||||
];
|
||||
@@ -178,7 +183,7 @@ class CommonHandler extends ExceptionHandler
|
||||
$code = empty($throwable->getCode()) ? CommonErrorCode::MODEL_NOT_FOUND : $throwable->getCode();
|
||||
$message = empty($throwable->getCode()) ? CommonErrorCode::getMessage(
|
||||
$code,
|
||||
['resource' => '资源']
|
||||
['resource' => '资源'],
|
||||
) : $throwable->getMessage();
|
||||
$data = [$code_name => $code, $message_name => $message];
|
||||
$status_code = RFC7231::NOT_FOUND;
|
||||
@@ -193,7 +198,7 @@ class CommonHandler extends ExceptionHandler
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => CommonErrorCode::getMessage(
|
||||
$is_testing ? $code : CommonErrorCode::SERVER_CACHE_REDIS_ERROR
|
||||
$is_testing ? $code : CommonErrorCode::SERVER_CACHE_REDIS_ERROR,
|
||||
),
|
||||
];
|
||||
}
|
||||
@@ -207,17 +212,39 @@ class CommonHandler extends ExceptionHandler
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => CommonErrorCode::getMessage(
|
||||
$is_testing ? $code : CommonErrorCode::SERVER_MESSAGE_EMAIL_ERROR
|
||||
$is_testing ? $code : CommonErrorCode::SERVER_MESSAGE_EMAIL_ERROR,
|
||||
),
|
||||
];
|
||||
}
|
||||
// 500 业务错误
|
||||
if ($throwable instanceof ApiMigrated) {
|
||||
$code = $throwable->getCode();
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => $throwable->getMessage(),
|
||||
];
|
||||
$headers = [
|
||||
'Location' => $throwable->getNewPath(),
|
||||
];
|
||||
}
|
||||
if ($throwable instanceof NotImplementedException || $throwable instanceof \Hyperf\Framework\Exception\NotImplementedException) {
|
||||
$code = $throwable->getCode();
|
||||
if ($code === 0) {
|
||||
$code = CommonErrorCode::BUSINESS_NOT_IMPLEMENTED;
|
||||
}
|
||||
$message = $throwable->getMessage() ?? CommonErrorCode::getMessage($code);
|
||||
$data = [
|
||||
$code_name => $code,
|
||||
$message_name => $message,
|
||||
];
|
||||
}
|
||||
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)
|
||||
CommonErrorCode::getMessage(CommonErrorCode::SERVER_ERROR),
|
||||
),
|
||||
];
|
||||
// 其他错误
|
||||
@@ -233,45 +260,47 @@ 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) {
|
||||
$response = $response->withStatus(
|
||||
$status_code ?? $throwable->status ?? $throwable->statusCode ?? RFC7231::INTERNAL_SERVER_ERROR
|
||||
$status_code ?? $throwable->status ?? $throwable->statusCode ?? RFC7231::INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
foreach ($headers ?? [] as $header => $value) {
|
||||
$response->withHeader($header, $value);
|
||||
}
|
||||
return $response->withBody(new SwooleStream($data));
|
||||
}
|
||||
|
||||
|
||||
29
src/Exceptions/NotImplementedException.php
Normal file
29
src/Exceptions/NotImplementedException.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* NotImplementedException.php@Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2025/8/2
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Singularity\HDK\Core\Exceptions;
|
||||
|
||||
use Hyperf\HttpMessage\Exception\HttpException;
|
||||
use Singularity\HDK\Core\Constants\CommonErrorCode;
|
||||
use Teapot\StatusCode\RFC\RFC7231;
|
||||
use Throwable;
|
||||
|
||||
final class NotImplementedException extends HttpException
|
||||
{
|
||||
public function __construct(
|
||||
int $code = CommonErrorCode::BUSINESS_NOT_IMPLEMENTED,
|
||||
?string $message = null,
|
||||
Throwable $previous = null,
|
||||
) {
|
||||
$message ??= CommonErrorCode::getMessage($code);
|
||||
parent::__construct(RFC7231::NOT_IMPLEMENTED, $message, $code, $previous);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user