From 07321498fc03e66ecec1ea5f545109b4e8004b70 Mon Sep 17 00:00:00 2001 From: Ilyas Salikhov Date: Tue, 15 Oct 2013 14:11:57 +0400 Subject: [PATCH] Error processing throw exceptions --- README.md | 14 ++-- lib/IntaroCrm/Exception/ApiException.php | 6 ++ lib/IntaroCrm/Exception/CurlException.php | 6 ++ lib/IntaroCrm/RestApi.php | 83 +++++++++++------------ 4 files changed, 60 insertions(+), 49 deletions(-) create mode 100644 lib/IntaroCrm/Exception/ApiException.php create mode 100644 lib/IntaroCrm/Exception/CurlException.php diff --git a/README.md b/README.md index 911c1bb..e0f12aa 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Add IntaroCRM REST API client in your composer.json: ```js { "require": { - "intarocrm/rest-api-client": "dev-master" + "intarocrm/rest-api-client": "1.1.*" } } ``` @@ -42,8 +42,14 @@ Constructor arguments are: ``` php -$orderTypes = $crmApiClient->orderTypesList(); +try { + $orderTypes = $crmApiClient->orderTypesList(); +} +catch (\IntaroCrm\Exception\CurlException $e) { + //$logger->addError('orderTypesList: connection error'); +} +catch (\IntaroCrm\Exception\ApiException $e) { + //$logger->addError('orderTypesList: ' . $e->getMessage()); +} -if (!is_null($crmApiClient->getLastError())) - return $crmApiClient->getLastError(); ``` diff --git a/lib/IntaroCrm/Exception/ApiException.php b/lib/IntaroCrm/Exception/ApiException.php new file mode 100644 index 0000000..75e4d8d --- /dev/null +++ b/lib/IntaroCrm/Exception/ApiException.php @@ -0,0 +1,6 @@ +parameters = array('apiKey' => $this->apiKey); } - - public function getStatusCode() - { - return $this->statusCode; - } - - /* Получение кода статуса и сообщения об ошибке */ - public function getLastError() - { - if (isset($this->response['errorMsg']) && isset($this->response['errors'])) - { - $result = $this->statusCode . ' ' . $this->response['errorMsg']; - foreach ($this->response['errors'] as $error) - $result .= ' ' . $error; - } - elseif (isset($this->response['errorMsg'])) - $result = $this->statusCode . ' ' . $this->response['errorMsg']; - else - $result = null; - return $result; - } - - /* Псообщения об ошибке */ - public function getLastErrorMessage() - { - return $this->response['errorMsg']; - } - - /* Методы для работы с заказами */ /** * Получение заказа по id @@ -440,7 +409,7 @@ class RestApi $result = $this->curlRequest($url); return $result; } - + /** * Обновление статистики * @@ -453,6 +422,29 @@ class RestApi return $result; } + protected function getErrorMessage($response) + { + $str = ''; + if (isset($response['message'])) + $str = $response['message']; + elseif (isset($response[0]['message'])) + $str = $response[0]['message']; + elseif (isset($response['error']) && isset($response['error']['message'])) + $str = $response['error']['message']; + elseif (isset($response['errorMsg'])) + $str = $response['errorMsg']; + + if (isset($response['errors']) && sizeof($response['errors'])) { + foreach ($response['errors'] as $error) + $str .= '. ' . $error; + } + + if (!strlen($str)) + return 'Application Error'; + + return $str; + } + protected function curlRequest($url, $method = 'GET', $format = 'json') { if ($method == 'GET' && !is_null($this->parameters)) @@ -472,28 +464,29 @@ class RestApi } $response = curl_exec($ch); - $this->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); unset($this->parameters); /* Сброс массива с параметрами */ $this->parameters = array('apiKey' => $this->apiKey); - if (curl_errno($ch)) - { - $this->response = array('errorMsg' => 'Curl error: ' . curl_error($ch)); - return null; - } + $errno = curl_errno($ch); + $error = curl_error($ch); curl_close($ch); - $result = (array)json_decode($response, true); - $this->response = $result; - if ($result['success'] == false) - return null; + if ($errno) + throw new Exception\CurlException($error, $errno); + + $result = json_decode($response, true); + + if ($statusCode >= 400 || isset($result['success']) && $result['success'] === false) { + throw new Exception\ApiException($this->getErrorMessage($result), $statusCode); + } unset($result['success']); + if (count($result) == 0) return true; + return reset($result); } -} - -?> \ No newline at end of file +} \ No newline at end of file