1
0
mirror of synced 2024-11-28 00:06:06 +03:00

Error processing throw exceptions

This commit is contained in:
Ilyas Salikhov 2013-10-15 14:11:57 +04:00
parent afc3ff1b45
commit 07321498fc
4 changed files with 60 additions and 49 deletions

View File

@ -17,7 +17,7 @@ Add IntaroCRM REST API client in your composer.json:
```js ```js
{ {
"require": { "require": {
"intarocrm/rest-api-client": "dev-master" "intarocrm/rest-api-client": "1.1.*"
} }
} }
``` ```
@ -42,8 +42,14 @@ Constructor arguments are:
``` php ``` 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();
``` ```

View File

@ -0,0 +1,6 @@
<?php
namespace IntaroCrm\Exception;
class ApiException extends \Exception
{
}

View File

@ -0,0 +1,6 @@
<?php
namespace IntaroCrm\Exception;
class CurlException extends \Exception
{
}

View File

@ -7,8 +7,6 @@ class RestApi
protected $apiKey; protected $apiKey;
protected $apiVersion = '1'; protected $apiVersion = '1';
protected $response;
protected $statusCode;
protected $parameters; protected $parameters;
/** /**
@ -22,35 +20,6 @@ class RestApi
$this->parameters = array('apiKey' => $this->apiKey); $this->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 * Получение заказа по id
@ -453,6 +422,29 @@ class RestApi
return $result; 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') protected function curlRequest($url, $method = 'GET', $format = 'json')
{ {
if ($method == 'GET' && !is_null($this->parameters)) if ($method == 'GET' && !is_null($this->parameters))
@ -472,28 +464,29 @@ class RestApi
} }
$response = curl_exec($ch); $response = curl_exec($ch);
$this->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
unset($this->parameters); unset($this->parameters);
/* Сброс массива с параметрами */ /* Сброс массива с параметрами */
$this->parameters = array('apiKey' => $this->apiKey); $this->parameters = array('apiKey' => $this->apiKey);
if (curl_errno($ch)) $errno = curl_errno($ch);
{ $error = curl_error($ch);
$this->response = array('errorMsg' => 'Curl error: ' . curl_error($ch));
return null;
}
curl_close($ch); curl_close($ch);
$result = (array)json_decode($response, true); if ($errno)
$this->response = $result; throw new Exception\CurlException($error, $errno);
if ($result['success'] == false)
return null; $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']); unset($result['success']);
if (count($result) == 0) if (count($result) == 0)
return true; return true;
return reset($result); return reset($result);
} }
} }
?>