Commit bdb3a68a authored by 谢宇轩's avatar 谢宇轩

fix: 缩减默认策略的范围

parent 5f2fcc25
......@@ -219,33 +219,37 @@ class Application implements ClientInterface
$rpcResult = [];
try {
$rpcResult = $this->option->errorHandlingPolicy()->process($response);
} catch (GuiltyResultException $exception) {
// 没有通过预期判定,转化为 SDK Exception
$this->lastRequestContext['error_message'] = $exception->getMessage();
$this->lastRequestContext['error_detail'] = $exception->getOptions(); // 获取错误的详情
if ($this->logger) {
$this->logger->warning("Error", $this->lastRequestContext);
}
throw new SdkException($exception->getMessage(), $request, $response, $exception->getOptions());
} catch (UnknowResultException $exception) {
// 无法解析,转化为 Application Exception
// 日志中保留 OUT PUT
$this->lastRequestContext['error_message'] = $exception->getMessage();
$this->lastRequestContext['error_detail'] = $exception->getResult(); // 获取错误的详情
if ($this->logger) {
$this->logger->error("sdk error, bad response content.", $this->lastRequestContext);
if ($response->getStatusCode() != 304) {
try {
$rpcResult = $this->option->errorHandlingPolicy()->process($response);
} catch (GuiltyResultException $exception) {
// 没有通过预期判定,转化为 SDK Exception
$this->lastRequestContext['error_message'] = $exception->getMessage();
$this->lastRequestContext['error_detail'] = $exception->getOptions(); // 获取错误的详情
if ($this->logger) {
$this->logger->warning("Error", $this->lastRequestContext);
}
throw new SdkException($exception->getMessage(), $request, $response, $exception->getOptions());
} catch (UnknowResultException $exception) {
// 无法解析,转化为 Application Exception
// 日志中保留 OUT PUT
$this->lastRequestContext['error_message'] = $exception->getMessage();
$this->lastRequestContext['error_detail'] = $exception->getResult(); // 获取错误的详情
if ($this->logger) {
$this->logger->error("sdk error, bad response content.", $this->lastRequestContext);
}
throw new ApplicationException("Content Format error.", $request, []);
}
throw new ApplicationException("Content Format error.", $request, []);
}
$version = $response->getHeader('ETag');
if (!empty($version)) {
$rpcResult = array_merge($rpcResult, [
'version' => $version[0]
]);
}
if ($this->logger) {
$this->logger->info("Success", $context);
}
......
......@@ -9,15 +9,13 @@ use Psr\Http\Message\ResponseInterface;
class DefaultErrorHandlingPolicy implements HandlingPolicyInterface
{
/**
* 处理API层面异常的规则
*
* @param ResponseInterface $response
* @return array<string, mixed>
*/
public function process(ResponseInterface $response): array
{
if ($response->getStatusCode() == 304) {
return [];
}
$responseInfo = $response->getBody()->getContents();
$rpcResult = json_decode($responseInfo, true);
......@@ -25,8 +23,9 @@ class DefaultErrorHandlingPolicy implements HandlingPolicyInterface
throw new UnknowResultException("Content Format error.", $responseInfo);
}
if (!empty($rpcResult['name']) && $response->getStatusCode() >= 400) {
$errorMessage = sprintf("%s : %s", $rpcResult['name'], $rpcResult['message'] ?? "");
$status = $rpcResult['status'] ?? 1;
if ($status != 0 && $response->getStatusCode() >= 400) {
$errorMessage = sprintf("Api error : %s",$rpcResult['message'] ?? "");
throw new GuiltyResultException($errorMessage, $rpcResult);
}
return $rpcResult;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment