diff --git a/src/Http/PsrHttpClient.php b/src/Http/PsrHttpClient.php index 0e886453..912d711e 100644 --- a/src/Http/PsrHttpClient.php +++ b/src/Http/PsrHttpClient.php @@ -85,7 +85,16 @@ protected function doRequest($url, array $data = null) $content = $response->getBody()->getContents(); - return self::jsonValidate($content); + $json = self::jsonValidate($content); + + if (!\in_array($response->getStatusCode(), [200, 304])) { + $errorDescription = array_key_exists('description', $json) ? $json['description'] : $response->getReasonPhrase(); + $errorParameters = array_key_exists('parameters', $json) ? $json['parameters'] : []; + + throw new HttpException($errorDescription, $response->getStatusCode(), null, $errorParameters); + } + + return $json; } /** diff --git a/src/Http/SymfonyHttpClient.php b/src/Http/SymfonyHttpClient.php index b24a30da..7dea7df1 100644 --- a/src/Http/SymfonyHttpClient.php +++ b/src/Http/SymfonyHttpClient.php @@ -5,6 +5,7 @@ use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\Part\Multipart\FormDataPart; use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; +use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface as SymfonyHttpClientInterface; use TelegramBot\Api\HttpException; @@ -50,7 +51,16 @@ protected function doRequest($url, array $data = null) try { return $response->toArray(); } catch (ExceptionInterface $exception) { - throw new HttpException($exception->getMessage(), $exception->getCode(), $exception); + if ($exception instanceof HttpExceptionInterface) { + $response = $exception->getResponse()->toArray(false); + $message = array_key_exists('description', $response) ? $response['description'] : $exception->getMessage(); + $parameters = array_key_exists('parameters', $response) ? $response['parameters'] : []; + } else { + $message = $exception->getMessage(); + $parameters = []; + } + + throw new HttpException($message, $exception->getCode(), $exception, $parameters); } }