Error processing throw exceptions
This commit is contained in:
parent
afc3ff1b45
commit
07321498fc
14
README.md
14
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();
|
||||
```
|
||||
|
6
lib/IntaroCrm/Exception/ApiException.php
Normal file
6
lib/IntaroCrm/Exception/ApiException.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace IntaroCrm\Exception;
|
||||
|
||||
class ApiException extends \Exception
|
||||
{
|
||||
}
|
6
lib/IntaroCrm/Exception/CurlException.php
Normal file
6
lib/IntaroCrm/Exception/CurlException.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace IntaroCrm\Exception;
|
||||
|
||||
class CurlException extends \Exception
|
||||
{
|
||||
}
|
@ -7,8 +7,6 @@ class RestApi
|
||||
protected $apiKey;
|
||||
protected $apiVersion = '1';
|
||||
|
||||
protected $response;
|
||||
protected $statusCode;
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
@ -22,35 +20,6 @@ class RestApi
|
||||
$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
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
}
|
Loading…
Reference in New Issue
Block a user