From d79ca543d5ef075cb1e0d71f9e2789e8f1683b63 Mon Sep 17 00:00:00 2001 From: Xiaohui Lam Date: Fri, 5 Jun 2020 10:44:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E4=BD=BF=E7=94=A8=20Laravel=20?= =?UTF-8?q?=E6=A1=86=E6=9E=B6=EF=BC=8C=E4=BD=BF=E7=94=A8=20ValidationExcep?= =?UTF-8?q?tion::withMessages=20=E6=9D=A5=E6=8A=9B=E5=87=BA=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Client.php | 75 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/src/Client.php b/src/Client.php index 24c2d29..b5d38d4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -9,7 +9,9 @@ use DigitalSign\Sdk\Resources\Product; use DigitalSign\Sdk\Traits\SignTrait; use GuzzleHttp\Client as GuzzleHttpClient; +use GuzzleHttp\Exception\ClientException; use GuzzleHttp\RequestOptions; +use Illuminate\Validation\ValidationException; use function GuzzleHttp\json_decode; @@ -93,36 +95,55 @@ public function __construct($accessKeyId, $accessKeySecret, $apiOrigin = null, $ */ public function __call($method, $arguments = []) { - $http = new GuzzleHttpClient([ - RequestOptions::CONNECT_TIMEOUT => $this->connectTimeout, - RequestOptions::READ_TIMEOUT => $this->readTimeout, - ]); - - $api = $arguments[0]; - $resource = '/' . $api; - - $parameters = isset($arguments[1]) ? $arguments[1] : []; - $parameters = $this->sign($resource, $parameters, $this->accessKeyId, $this->accessKeySecret); - - $uri = $this->apiOrigin . $resource; - - $response = $http->{$method}($uri, [ - ($method == 'get' ? 'query' : RequestOptions::JSON) => $parameters, - ]); - - $json = json_decode($response->getBody()); + try { + $http = new GuzzleHttpClient([ + RequestOptions::CONNECT_TIMEOUT => $this->connectTimeout, + RequestOptions::READ_TIMEOUT => $this->readTimeout, + ]); + + $api = $arguments[0]; + $resource = '/' . $api; + + $parameters = isset($arguments[1]) ? $arguments[1] : []; + $parameters = $this->sign($resource, $parameters, $this->accessKeyId, $this->accessKeySecret); + + $uri = $this->apiOrigin . $resource; + + $response = $http->{$method}($uri, [ + //($method == 'get' ? 'query' : RequestOptions::JSON) => $parameters, + ]); + + $json = json_decode($response->getBody()->__toString()); + + if (!isset($json->success) || !$json->success) { + $exception_class = RequestException::class; + $map = static::CODE_EXCEPTION_MAP; + if (!isset($json->error_code)) { + throw new RequestException('未知错误', -1); + } + if (isset($map[$json->error_code])) { + $exception_class = $map[$json->error_code]; + } + throw new $exception_class(isset($json->message) ? $json->message : '请求接口出错', isset($json->error_code) ? $json->error_code : -1); + } + return $json->data; + } catch (ClientException $e) { + // 若不存在 Laravel's ValidationException 类,或者版本太低没有 withMessages 方法,抛出Guzzle的异常 + if (!class_exists(ValidationException::class) || !method_exists(ValidationException::class, 'withMessages')) { + throw $e; + } - if (!isset($json->success) || !$json->success) { - $exception_class = RequestException::class; - $map = static::CODE_EXCEPTION_MAP; - if (!isset($json->error_code)) { - throw new RequestException('未知错误', -1); + $response = $e->getResponse(); + if ($response->getStatusCode() !== 412) { + throw $e; } - if (isset($map[$json->error_code])) { - $exception_class = $map[$json->error_code]; + + $data = json_decode($response->getBody()->__toString(), true); + if (JSON_ERROR_NONE !== json_last_error() || !isset($data['message'])) { + throw new ClientException('JSON DECODE ERROR', $e->getRequest(), $e->getResponse(), $e); } - throw new $exception_class(isset($json->message) ? $json->message : '请求接口出错', isset($json->error_code) ? $json->error_code : -1); + + throw ValidationException::withMessages($data['message']); } - return $json->data; } }