From 06fa9c8fc1e2e0976ab9883533ae1bd276cabf4f Mon Sep 17 00:00:00 2001 From: Dane Powell Date: Tue, 2 May 2023 12:10:19 -0700 Subject: [PATCH] CLI-1026: Catch malformed responses (#382) --- src/Connector/Client.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Connector/Client.php b/src/Connector/Client.php index 7db58014..719e5ea0 100644 --- a/src/Connector/Client.php +++ b/src/Connector/Client.php @@ -2,6 +2,7 @@ namespace AcquiaCloudApi\Connector; +use http\Exception\RuntimeException; use Psr\Http\Message\ResponseInterface; use GuzzleHttp\Exception\BadResponseException; use AcquiaCloudApi\Exception\ApiErrorException; @@ -152,7 +153,15 @@ public function processResponse(ResponseInterface $response): mixed { $body_json = $response->getBody(); - $body = json_decode($body_json); + $body = json_decode($body_json, null, 512, JSON_THROW_ON_ERROR); + if (is_null($body)) { + throw new RuntimeException( + 'Response contained an empty body. Status ' + . $response->getStatusCode() + . '. Request ID ' + . $response->getHeaderLine('X-Request-Id') + ); + } if (property_exists($body, '_embedded') && property_exists($body->_embedded, 'items')) { return $body->_embedded->items;