mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 05:55:12 +08:00
fix(exceptions): 处理 HTTP 客户端和服务器异常
- 在 CommonHandler 中添加了对 GuzzleHttp 客户端和服务器异常的处理 - 解析异常响应内容,提取错误代码和消息 - 根据异常类型设置相应的状态码 Signed-off-by: 李东云 <dongyu.li@luxcreo.ai>
This commit is contained in:
1
.idea/HDK-Core.iml
generated
1
.idea/HDK-Core.iml
generated
@@ -4,7 +4,6 @@
|
|||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="Singularity\HDK\Core\" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="Singularity\HDK\Core\" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="Singularity\HDK\Test\Core\" />
|
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="Singularity\HDK\Test\Core\" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
|
||||||
<excludeFolder url="file://$MODULE_DIR$/runtime" />
|
<excludeFolder url="file://$MODULE_DIR$/runtime" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/vendor/adbario/php-dot-notation" />
|
<excludeFolder url="file://$MODULE_DIR$/vendor/adbario/php-dot-notation" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/vendor/alibabacloud/credentials" />
|
<excludeFolder url="file://$MODULE_DIR$/vendor/alibabacloud/credentials" />
|
||||||
|
|||||||
1312
composer.lock
generated
1312
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -13,6 +13,8 @@ declare(strict_types=1);
|
|||||||
namespace Singularity\HDK\Core\Exceptions\Handler;
|
namespace Singularity\HDK\Core\Exceptions\Handler;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use GuzzleHttp\Exception\ClientException;
|
||||||
|
use GuzzleHttp\Exception\ServerException;
|
||||||
use Hyperf\Codec\Json;
|
use Hyperf\Codec\Json;
|
||||||
use Hyperf\Database\Exception\QueryException;
|
use Hyperf\Database\Exception\QueryException;
|
||||||
use Hyperf\Database\Model\ModelNotFoundException;
|
use Hyperf\Database\Model\ModelNotFoundException;
|
||||||
@@ -93,6 +95,26 @@ class CommonHandler extends ExceptionHandler
|
|||||||
$data['details'] = ['sql' => $throwable->getSql(), 'error' => $throwable->getMessage()];
|
$data['details'] = ['sql' => $throwable->getSql(), 'error' => $throwable->getMessage()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ($throwable instanceof ClientException) {
|
||||||
|
$content = $throwable->getResponse()->getBody()->getContents();
|
||||||
|
|
||||||
|
['errorCode' => $code, 'errorMsg' => $msg] = Json::decode($content);
|
||||||
|
$data = [
|
||||||
|
$code_name => $code,
|
||||||
|
$message_name => $msg,
|
||||||
|
];
|
||||||
|
$status_code = 400;
|
||||||
|
}
|
||||||
|
if ($throwable instanceof ServerException) {
|
||||||
|
$content = $throwable->getResponse()->getBody()->getContents();
|
||||||
|
|
||||||
|
['errorCode' => $code, 'errorMsg' => $msg] = Json::decode($content);
|
||||||
|
$data = [
|
||||||
|
$code_name => $code,
|
||||||
|
$message_name => $msg,
|
||||||
|
];
|
||||||
|
$status_code = 500;
|
||||||
|
}
|
||||||
// 101 请求方式错误
|
// 101 请求方式错误
|
||||||
if ($throwable instanceof MethodNotAllowedHttpException) {
|
if ($throwable instanceof MethodNotAllowedHttpException) {
|
||||||
$message = explode(': ', $throwable->getMessage());
|
$message = explode(': ', $throwable->getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user