|
6 | 6 | use Http\Client\Common\HttpMethodsClient; |
7 | 7 | use Http\Client\Exception as HttpClientException; |
8 | 8 | use Http\Client\Exception\HttpException; |
| 9 | +use Http\Discovery\Psr17FactoryDiscovery; |
| 10 | +use JsonException; |
9 | 11 | use Psr\Http\Client\ClientInterface; |
10 | 12 | use Psr\Http\Message\StreamInterface; |
11 | 13 | use Psr\Log\LoggerInterface; |
12 | | -use Http\Discovery\Psr17FactoryDiscovery; |
13 | 14 | use Typesense\Exceptions\HTTPStatus0Error; |
14 | 15 | use Typesense\Exceptions\ObjectAlreadyExists; |
15 | 16 | use Typesense\Exceptions\ObjectNotFound; |
@@ -255,16 +256,21 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr |
255 | 256 | $this->setNodeHealthCheck($node, true); |
256 | 257 | } |
257 | 258 |
|
| 259 | + $responseContents = $response->getBody() |
| 260 | + ->getContents(); |
| 261 | + |
258 | 262 | if (!(200 <= $statusCode && $statusCode < 300)) { |
259 | | - $errorMessage = json_decode($response->getBody() |
260 | | - ->getContents(), true, 512, JSON_THROW_ON_ERROR)['message'] ?? 'API error.'; |
| 263 | + try { |
| 264 | + $responseContents = json_decode($responseContents, true, 512, JSON_THROW_ON_ERROR); |
| 265 | + $errorMessage = $responseContents['message'] ?? 'API error.'; |
| 266 | + } catch (JsonException $exception) { |
| 267 | + $errorMessage = $responseContents ?? 'API error.'; |
| 268 | + } |
261 | 269 | throw $this->getException($statusCode) |
262 | 270 | ->setMessage($errorMessage); |
263 | 271 | } |
264 | 272 |
|
265 | | - return $asJson ? json_decode($response->getBody() |
266 | | - ->getContents(), true, 512, JSON_THROW_ON_ERROR) : $response->getBody() |
267 | | - ->getContents(); |
| 273 | + return $asJson ? json_decode($responseContents, true, 512, JSON_THROW_ON_ERROR) : $responseContents; |
268 | 274 | } catch (HttpException $exception) { |
269 | 275 | $statusCode = $exception->getResponse()->getStatusCode(); |
270 | 276 |
|
|
0 commit comments