Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/ApiCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Exception as HttpClientException;
use Http\Client\Exception\HttpException;
use Http\Discovery\Psr17FactoryDiscovery;
use JsonException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerInterface;
use Http\Discovery\Psr17FactoryDiscovery;
use Typesense\Exceptions\HTTPStatus0Error;
use Typesense\Exceptions\ObjectAlreadyExists;
use Typesense\Exceptions\ObjectNotFound;
Expand Down Expand Up @@ -255,16 +256,21 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
$this->setNodeHealthCheck($node, true);
}

$responseContents = $response->getBody()
->getContents();

if (!(200 <= $statusCode && $statusCode < 300)) {
$errorMessage = json_decode($response->getBody()
->getContents(), true, 512, JSON_THROW_ON_ERROR)['message'] ?? 'API error.';
try {
$responseContents = json_decode($responseContents, true, 512, JSON_THROW_ON_ERROR);
$errorMessage = $responseContents['message'] ?? 'API error.';
} catch (JsonException $exception) {
$errorMessage = $responseContents ?? 'API error.';
}
throw $this->getException($statusCode)
->setMessage($errorMessage);
}

return $asJson ? json_decode($response->getBody()
->getContents(), true, 512, JSON_THROW_ON_ERROR) : $response->getBody()
->getContents();
return $asJson ? json_decode($responseContents, true, 512, JSON_THROW_ON_ERROR) : $responseContents;
} catch (HttpException $exception) {
$statusCode = $exception->getResponse()->getStatusCode();

Expand Down