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

fix: 缩减默认策略的范围

parent 5f2fcc25
......@@ -219,6 +219,7 @@ class Application implements ClientInterface
$rpcResult = [];
if ($response->getStatusCode() != 304) {
try {
$rpcResult = $this->option->errorHandlingPolicy()->process($response);
} catch (GuiltyResultException $exception) {
......@@ -239,6 +240,8 @@ class Application implements ClientInterface
}
throw new ApplicationException("Content Format error.", $request, []);
}
}
$version = $response->getHeader('ETag');
if (!empty($version)) {
......@@ -246,6 +249,7 @@ class Application implements ClientInterface
'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