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

fix: 缩减默认策略的范围

parent 5f2fcc25
...@@ -219,6 +219,7 @@ class Application implements ClientInterface ...@@ -219,6 +219,7 @@ class Application implements ClientInterface
$rpcResult = []; $rpcResult = [];
if ($response->getStatusCode() != 304) {
try { try {
$rpcResult = $this->option->errorHandlingPolicy()->process($response); $rpcResult = $this->option->errorHandlingPolicy()->process($response);
} catch (GuiltyResultException $exception) { } catch (GuiltyResultException $exception) {
...@@ -239,6 +240,8 @@ class Application implements ClientInterface ...@@ -239,6 +240,8 @@ class Application implements ClientInterface
} }
throw new ApplicationException("Content Format error.", $request, []); throw new ApplicationException("Content Format error.", $request, []);
} }
}
$version = $response->getHeader('ETag'); $version = $response->getHeader('ETag');
if (!empty($version)) { if (!empty($version)) {
...@@ -246,6 +249,7 @@ class Application implements ClientInterface ...@@ -246,6 +249,7 @@ class Application implements ClientInterface
'version' => $version[0] 'version' => $version[0]
]); ]);
} }
if ($this->logger) { if ($this->logger) {
$this->logger->info("Success", $context); $this->logger->info("Success", $context);
} }
......
...@@ -9,15 +9,13 @@ use Psr\Http\Message\ResponseInterface; ...@@ -9,15 +9,13 @@ use Psr\Http\Message\ResponseInterface;
class DefaultErrorHandlingPolicy implements HandlingPolicyInterface class DefaultErrorHandlingPolicy implements HandlingPolicyInterface
{ {
/** /**
* 处理API层面异常的规则
*
* @param ResponseInterface $response * @param ResponseInterface $response
* @return array<string, mixed> * @return array<string, mixed>
*/ */
public function process(ResponseInterface $response): array public function process(ResponseInterface $response): array
{ {
if ($response->getStatusCode() == 304) {
return [];
}
$responseInfo = $response->getBody()->getContents(); $responseInfo = $response->getBody()->getContents();
$rpcResult = json_decode($responseInfo, true); $rpcResult = json_decode($responseInfo, true);
...@@ -25,8 +23,9 @@ class DefaultErrorHandlingPolicy implements HandlingPolicyInterface ...@@ -25,8 +23,9 @@ class DefaultErrorHandlingPolicy implements HandlingPolicyInterface
throw new UnknowResultException("Content Format error.", $responseInfo); throw new UnknowResultException("Content Format error.", $responseInfo);
} }
if (!empty($rpcResult['name']) && $response->getStatusCode() >= 400) { $status = $rpcResult['status'] ?? 1;
$errorMessage = sprintf("%s : %s", $rpcResult['name'], $rpcResult['message'] ?? ""); if ($status != 0 && $response->getStatusCode() >= 400) {
$errorMessage = sprintf("Api error : %s",$rpcResult['message'] ?? "");
throw new GuiltyResultException($errorMessage, $rpcResult); throw new GuiltyResultException($errorMessage, $rpcResult);
} }
return $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