Merge pull request #491 from FindTheBest/develop_2.0

PHP Bindings: exposing response and response info in Exceptions
This commit is contained in:
Tony Tam 2015-03-12 12:23:34 -07:00
commit 6c57d6a76e

View File

@ -127,19 +127,19 @@ class APIClient {
// Handle the response // Handle the response
if ($response_info['http_code'] == 0) { if ($response_info['http_code'] == 0) {
throw new Exception("TIMEOUT: api call to " . $url . throw new APIClientException("TIMEOUT: api call to " . $url .
" took more than 5s to return" ); " took more than 5s to return", 0, $response_info, $response);
} else if ($response_info['http_code'] == 200) { } else if ($response_info['http_code'] == 200) {
$data = json_decode($response); $data = json_decode($response);
} else if ($response_info['http_code'] == 401) { } else if ($response_info['http_code'] == 401) {
throw new Exception("Unauthorized API request to " . $url . throw new APIClientException("Unauthorized API request to " . $url .
": ".json_decode($response)->message ); ": " . json_decode($response)->message, 0, $response_info, $response);
} else if ($response_info['http_code'] == 404) { } else if ($response_info['http_code'] == 404) {
$data = null; $data = null;
} else { } else {
throw new Exception("Can't connect to the api: " . $url . throw new APIClientException("Can't connect to the api: " . $url .
" response code: " . " response code: " .
$response_info['http_code']); $response_info['http_code'], 0, $response_info, $response);
} }
return $data; return $data;
} }
@ -256,3 +256,20 @@ class APIClient {
} }
class APIClientException extends Exception {
protected $response, $response_info;
public class __construct($message="", $code=0, $response_info=null, $response=null) {
parent::__construct($message, $code);
$this->response_info = $response_info;
$this->response = $response;
}
public function getResponse() {
return $this->response;
}
public function getResponseInfo() {
return $this->response_info;
}
}