Shows response message on 400, 401 and 404 errors

Adds the actual response message to the errors thrown on 400, 401 and
404 response codes. This provides a lot more useful info than the
current messages. The message doesn’t really give you much to go one. I
spent hours trying to find what I did wrong, double checking my API
keys and looking up the error on google.
This commit is contained in:
CupOfTea696 2015-01-13 20:55:05 +01:00 committed by Jesse Spears
parent fbe34ce59a
commit e1406e3936

View File

@ -184,13 +184,13 @@ class RestClient {
$result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData; $result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData;
} }
elseif($httpResponseCode == 400){ elseif($httpResponseCode == 400){
throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS); throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj));
} }
elseif($httpResponseCode == 401){ elseif($httpResponseCode == 401){
throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS);
} }
elseif($httpResponseCode == 404){ elseif($httpResponseCode == 404){
throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT); throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj));
} }
else{ else{
throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody());
@ -199,6 +199,18 @@ class RestClient {
return $result; return $result;
} }
/**
* @param \Guzzle\Http\Message\Response $responseObj
* @return string
*/
protected function getResponseExceptionMessage(\Guzzle\Http\Message\Response $responseObj){
$body = (string)$responseObj->getBody();
$response = json_decode($body);
if (json_last_error() == JSON_ERROR_NONE && isset($response->message)) {
return " " . $response->message;
}
}
/** /**
* @param string $apiEndpoint * @param string $apiEndpoint
* @param string $apiVersion * @param string $apiVersion