From e67ef95d5b7fa1cd00093f89b783ca07ce41d548 Mon Sep 17 00:00:00 2001 From: z38 Date: Mon, 20 Feb 2017 21:04:05 +0100 Subject: [PATCH] Avoid fatal error when deserializing unauthorized requests (#282) --- src/Mailgun/Api/HttpApi.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Mailgun/Api/HttpApi.php b/src/Mailgun/Api/HttpApi.php index 67b7ca0..c1f61b0 100644 --- a/src/Mailgun/Api/HttpApi.php +++ b/src/Mailgun/Api/HttpApi.php @@ -12,6 +12,7 @@ namespace Mailgun\Api; use Http\Client\Exception as HttplugException; use Http\Client\HttpClient; use Mailgun\Deserializer\ResponseDeserializer; +use Mailgun\Exception\HttpClientException; use Mailgun\Exception\HttpServerException; use Mailgun\RequestBuilder; use Mailgun\Resource\Api\ErrorResponse; @@ -59,14 +60,18 @@ abstract class HttpApi * @param ResponseInterface $response * @param string $className * + * @throws HttpClientException + * * @return object $class */ protected function safeDeserialize(ResponseInterface $response, $className) { - if ($response->getStatusCode() !== 200) { - return $this->deserializer->deserialize($response, ErrorResponse::class); - } else { + if ($response->getStatusCode() === 200) { return $this->deserializer->deserialize($response, $className); + } elseif ($response->getStatusCode() === 401) { + throw HttpClientException::unauthorized(); + } else { + return $this->deserializer->deserialize($response, ErrorResponse::class); } }