diff --git a/lib/RetailCrm/Exception/NotFoundException.php b/lib/RetailCrm/Exception/NotFoundException.php new file mode 100644 index 0000000..cec6311 --- /dev/null +++ b/lib/RetailCrm/Exception/NotFoundException.php @@ -0,0 +1,24 @@ +checkResponse($curlHandler, $method); return new ApiResponse($statusCode, $responseBody); } @@ -178,4 +166,38 @@ class Client { $this->options = $options; } + + /** + * @param $curlHandler + * @param $method + * + * @return mixed + */ + private function checkResponse($curlHandler, $method) + { + $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); + $contentType = curl_getinfo($curlHandler, CURLINFO_CONTENT_TYPE); + + if (503 === $statusCode) { + throw new LimitException("Service temporary unavailable"); + } + + if ( + 404 === $statusCode + || ('GET' !== $method && 405 === $statusCode && false !== stripos($contentType, 'text/html')) + ) { + throw new NotFoundException("Account does not exist"); + } + + $errno = curl_errno($curlHandler); + $error = curl_error($curlHandler); + + curl_close($curlHandler); + + if ($errno) { + throw new CurlException($error, $errno); + } + + return $statusCode; + } }