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

fix: 缩减默认策略的范围

parent 5f2fcc25
...@@ -219,33 +219,37 @@ class Application implements ClientInterface ...@@ -219,33 +219,37 @@ class Application implements ClientInterface
$rpcResult = []; $rpcResult = [];
try { if ($response->getStatusCode() != 304) {
$rpcResult = $this->option->errorHandlingPolicy()->process($response); try {
} catch (GuiltyResultException $exception) { $rpcResult = $this->option->errorHandlingPolicy()->process($response);
// 没有通过预期判定,转化为 SDK Exception } catch (GuiltyResultException $exception) {
$this->lastRequestContext['error_message'] = $exception->getMessage(); // 没有通过预期判定,转化为 SDK Exception
$this->lastRequestContext['error_detail'] = $exception->getOptions(); // 获取错误的详情 $this->lastRequestContext['error_message'] = $exception->getMessage();
if ($this->logger) { $this->lastRequestContext['error_detail'] = $exception->getOptions(); // 获取错误的详情
$this->logger->warning("Error", $this->lastRequestContext); if ($this->logger) {
} $this->logger->warning("Error", $this->lastRequestContext);
throw new SdkException($exception->getMessage(), $request, $response, $exception->getOptions()); }
} catch (UnknowResultException $exception) { throw new SdkException($exception->getMessage(), $request, $response, $exception->getOptions());
// 无法解析,转化为 Application Exception } catch (UnknowResultException $exception) {
// 日志中保留 OUT PUT // 无法解析,转化为 Application Exception
$this->lastRequestContext['error_message'] = $exception->getMessage(); // 日志中保留 OUT PUT
$this->lastRequestContext['error_detail'] = $exception->getResult(); // 获取错误的详情 $this->lastRequestContext['error_message'] = $exception->getMessage();
if ($this->logger) { $this->lastRequestContext['error_detail'] = $exception->getResult(); // 获取错误的详情
$this->logger->error("sdk error, bad response content.", $this->lastRequestContext); 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'); $version = $response->getHeader('ETag');
if (!empty($version)) { if (!empty($version)) {
$rpcResult = array_merge($rpcResult, [ $rpcResult = array_merge($rpcResult, [
'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