Skip to content

Commit 76f1df1

Browse files
committed
removed throwing error on constructor for response class
changed guzzleClient execute method start using RequestException instead response injected to qdrant exception fix for the status code
1 parent 113f2ad commit 76f1df1

5 files changed

Lines changed: 20 additions & 31 deletions

File tree

src/Exception/ServerException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
namespace Qdrant\Exception;
1010

1111
use Qdrant\Response;
12+
use RuntimeException;
1213

13-
class ServerException extends \RuntimeException
14+
class ServerException extends RuntimeException
1415
{
1516
protected Response $response;
1617

src/Http/GuzzleClient.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Qdrant\Http;
77

88
use GuzzleHttp\Client;
9-
use GuzzleHttp\Exception\ClientException;
9+
use GuzzleHttp\Exception\RequestException;
1010
use JsonException;
1111
use Psr\Http\Client\ClientExceptionInterface;
1212
use Psr\Http\Message\RequestInterface;
@@ -51,15 +51,19 @@ public function execute(RequestInterface $request): Response
5151
{
5252
$request = $this->prepareHeaders($request);
5353
try {
54-
$res = $this->client->sendRequest($request);
54+
$res = $this->client->send($request);
5555

5656
return new Response($res);
57-
} catch (ClientException $e) {
57+
} catch (RequestException $e) {
5858
$statusCode = $e->getResponse()->getStatusCode();
5959
if ($statusCode >= 400 && $statusCode < 500) {
60-
throw new InvalidArgumentException($e->getMessage());
60+
$errorResponse = new Response($e->getResponse());
61+
throw (new InvalidArgumentException($e->getMessage(), $statusCode))
62+
->setResponse($errorResponse);
6163
} elseif ($statusCode >= 500) {
62-
throw new ServerException($e->getMessage());
64+
$errorResponse = new Response($e->getResponse());
65+
throw (new ServerException($e->getMessage(), $statusCode))
66+
->setResponse($errorResponse);
6367
}
6468
}
6569
}

src/Response.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ public function __construct(protected ResponseInterface $response)
3131
'content' => $response->getBody()->getContents()
3232
];
3333
}
34-
35-
if ($this->response->getStatusCode() >= 400 && $this->response->getStatusCode() < 500) {
36-
throw (new InvalidArgumentException(
37-
$this->raw['status']['error'] ?? 'Invalid argument exception',
38-
$this->response->getStatusCode()))->setResponse($this);
39-
}
40-
41-
if ($this->response->getStatusCode() >= 500) {
42-
throw (new ServerException('Server Exception', $this->response->getStatusCode()))->setResponse($this);
43-
}
4434
}
4535

4636
public function __toArray(): array

tests/Integration/Endpoints/ClusterTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Qdrant\Endpoints\Cluster;
1010
use Qdrant\Exception\InvalidArgumentException;
11+
use Qdrant\Exception\ServerException;
1112
use Qdrant\Tests\Integration\AbstractIntegration;
1213

1314
class ClusterTest extends AbstractIntegration
@@ -29,15 +30,12 @@ public function testClusterInfo(): void
2930
*/
3031
public function testClusterRecover(): void
3132
{
33+
$this->expectException(ServerException::class);
34+
$this->expectExceptionMessage('500 Internal Server Error');
35+
$this->expectExceptionCode(500);
36+
3237
$cluster = new Cluster($this->client);
3338
$response = $cluster->recover();
34-
35-
$this->assertArrayHasKey('status', $response);
36-
$this->assertArrayHasKey('time', $response);
37-
$this->assertEquals(
38-
'Service internal error: Qdrant is running in standalone mode',
39-
$response['status']['error']
40-
);
4139
}
4240

4341
/**

tests/Unit/ResponseTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ public function testConstructResponse2(): void
4848

4949
public function testConstructResponseWith4xxHttpCode(): void
5050
{
51-
$this->expectException(InvalidArgumentException::class);
52-
$this->expectExceptionMessage('Invalid argument exception');
53-
$this->expectExceptionCode(418);
5451
$httpResponse = new HttpResponse(
5552
418,
5653
[
@@ -59,20 +56,19 @@ public function testConstructResponseWith4xxHttpCode(): void
5956
Utils::streamFor(json_encode(['foo' => 'bar']))
6057
);
6158

62-
new Response($httpResponse);
59+
$response = new Response($httpResponse);
60+
$this->assertEquals('bar', $response['foo']);
6361
}
6462

6563
public function testConstructResponseWith5xxHttpCode(): void
6664
{
67-
$this->expectException(ServerException::class);
68-
$this->expectExceptionMessage('Server Exception');
69-
$this->expectExceptionCode(510);
7065
$httpResponse = new HttpResponse(
7166
510,
7267
[],
7368
Utils::streamFor(json_encode(['foo' => 'bar']))
7469
);
7570

76-
new Response($httpResponse);
71+
$response = new Response($httpResponse);
72+
$this->assertEquals('{"foo":"bar"}', $response['content']);
7773
}
7874
}

0 commit comments

Comments
 (0)