diff --git a/src/Mailgun/Exception/HttpClientException.php b/src/Mailgun/Exception/HttpClientException.php index 8db2469..1dedffc 100644 --- a/src/Mailgun/Exception/HttpClientException.php +++ b/src/Mailgun/Exception/HttpClientException.php @@ -22,6 +22,11 @@ final class HttpClientException extends \RuntimeException implements Exception */ private $response; + /** + * @var array + */ + private $responseBody; + /** * @param string $message * @param int $code @@ -30,7 +35,16 @@ final class HttpClientException extends \RuntimeException implements Exception public function __construct($message, $code, ResponseInterface $response = null) { parent::__construct($message, $code); - $this->response = $response; + + if ($response) { + $this->response = $response; + $body = $response->getBody()->__toString(); + if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) { + $this->responseBody['message'] = $body; + } else { + $this->responseBody = json_decode($body, true); + } + } } public static function badRequest(ResponseInterface $response = null) @@ -60,4 +74,12 @@ final class HttpClientException extends \RuntimeException implements Exception { return $this->response; } + + /** + * @return array + */ + public function getResponseBody() + { + return $this->responseBody; + } }