From 1203e6654d4623443298c3ffb31f76ceef735e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=20=D0=93=D1=83=D1=81=D0=B5=D0=B2?= Date: Mon, 17 Aug 2015 14:54:43 +0300 Subject: [PATCH] Added HttpException --- src/BotApi.php | 58 ++++++++++++++++++++++++++++++++++++++++++- src/HttpException.php | 8 ++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/HttpException.php diff --git a/src/BotApi.php b/src/BotApi.php index 14f59923..f3fc2366 100644 --- a/src/BotApi.php +++ b/src/BotApi.php @@ -8,6 +8,56 @@ class BotApi { + public static $codes = array( + // Informational 1xx + 100 => 'Continue', + 101 => 'Switching Protocols', + // Success 2xx + 200 => 'OK', + 201 => 'Created', + 202 => 'Accepted', + 203 => 'Non-Authoritative Information', + 204 => 'No Content', + 205 => 'Reset Content', + 206 => 'Partial Content', + // Redirection 3xx + 300 => 'Multiple Choices', + 301 => 'Moved Permanently', + 302 => 'Found', // 1.1 + 303 => 'See Other', + 304 => 'Not Modified', + 305 => 'Use Proxy', + // 306 is deprecated but reserved + 307 => 'Temporary Redirect', + // Client Error 4xx + 400 => 'Bad Request', + 401 => 'Unauthorized', + 402 => 'Payment Required', + 403 => 'Forbidden', + 404 => 'Not Found', + 405 => 'Method Not Allowed', + 406 => 'Not Acceptable', + 407 => 'Proxy Authentication Required', + 408 => 'Request Timeout', + 409 => 'Conflict', + 410 => 'Gone', + 411 => 'Length Required', + 412 => 'Precondition Failed', + 413 => 'Request Entity Too Large', + 414 => 'Request-URI Too Long', + 415 => 'Unsupported Media Type', + 416 => 'Requested Range Not Satisfiable', + 417 => 'Expectation Failed', + // Server Error 5xx + 500 => 'Internal Server Error', + 501 => 'Not Implemented', + 502 => 'Bad Gateway', + 503 => 'Service Unavailable', + 504 => 'Gateway Timeout', + 505 => 'HTTP Version Not Supported', + 509 => 'Bandwidth Limit Exceeded' + ); + /** * Url prefix */ @@ -83,7 +133,13 @@ public function call($method, array $data = null) curl_setopt_array($this->curl, $options); - $response = json_decode(curl_exec($this->curl), $this->returnArray); + $result = curl_exec($this->curl); + + if(($httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE)) && $httpCode !== 200) { + throw new HttpException(self::$codes[$httpCode], $httpCode); + } + + $response = json_decode($result, $this->returnArray); if ($this->returnArray) { if (!$response['ok']) { diff --git a/src/HttpException.php b/src/HttpException.php new file mode 100644 index 00000000..5a2de926 --- /dev/null +++ b/src/HttpException.php @@ -0,0 +1,8 @@ +