Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #71: Show response message on 400, 401 and 404. #72

Merged
merged 7 commits into from
May 5, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Mailgun/Connection/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ public function responseHandler($responseObj){
$result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData;
}
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){
throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS);
}
elseif($httpResponseCode == 404){
throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT);
throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj));
}
else{
throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody());
Expand All @@ -199,6 +199,18 @@ public function responseHandler($responseObj){
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 $apiVersion
Expand Down