From e1406e393697d873aad34f64fa53477c4cb2a134 Mon Sep 17 00:00:00 2001 From: CupOfTea696 Date: Tue, 13 Jan 2015 20:55:05 +0100 Subject: [PATCH] Shows response message on 400, 401 and 404 errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/Mailgun/Connection/RestClient.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index e5505f0..12b7c56 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -184,13 +184,13 @@ class RestClient { $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()); @@ -199,6 +199,18 @@ class RestClient { 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