Skip to content

Commit 113f2ad

Browse files
committed
ServerException changed as extending RuntimeException. And test improved for response class.
1 parent 0d6c4fe commit 113f2ad

4 files changed

Lines changed: 84 additions & 5 deletions

File tree

src/Exception/ServerException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use Qdrant\Response;
1212

13-
class ServerException extends \Exception
13+
class ServerException extends \RuntimeException
1414
{
1515
protected Response $response;
1616

src/Http/GuzzleClient.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use GuzzleHttp\Client;
99
use GuzzleHttp\Exception\ClientException;
10-
use GuzzleHttp\Exception\GuzzleException;
1110
use JsonException;
1211
use Psr\Http\Client\ClientExceptionInterface;
1312
use Psr\Http\Message\RequestInterface;

src/Response.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ public function __construct(protected ResponseInterface $response)
3333
}
3434

3535
if ($this->response->getStatusCode() >= 400 && $this->response->getStatusCode() < 500) {
36-
throw (new InvalidArgumentException($this->raw['status']['error'] ?? 'Invalid argument exception'))->setResponse($this);
36+
throw (new InvalidArgumentException(
37+
$this->raw['status']['error'] ?? 'Invalid argument exception',
38+
$this->response->getStatusCode()))->setResponse($this);
3739
}
3840

39-
if ($this->response->getStatusCode() >= 500 && $this->response->getStatusCode() < 500) {
40-
throw (new ServerException())->setResponse($this);
41+
if ($this->response->getStatusCode() >= 500) {
42+
throw (new ServerException('Server Exception', $this->response->getStatusCode()))->setResponse($this);
4143
}
4244
}
4345

tests/Unit/ResponseTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* @since Mar 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Tests\Unit;
8+
9+
use GuzzleHttp\Psr7\Response as HttpResponse;
10+
use GuzzleHttp\Psr7\Utils;
11+
use PHPUnit\Framework\TestCase;
12+
use Psr\Http\Message\ResponseInterface;
13+
use Qdrant\Exception\InvalidArgumentException;
14+
use Qdrant\Exception\ServerException;
15+
use Qdrant\Response;
16+
17+
class ResponseTest extends TestCase
18+
{
19+
public function testConstructResponse(): void
20+
{
21+
$httpResponse = new HttpResponse(
22+
200,
23+
[
24+
'content-type' => 'application/json'
25+
],
26+
Utils::streamFor(json_encode(['foo' => 'bar']))
27+
);
28+
29+
$response = new Response($httpResponse);
30+
31+
$this->assertEquals('bar', $response['foo']);
32+
}
33+
34+
public function testConstructResponse2(): void
35+
{
36+
$httpResponse = new HttpResponse(
37+
200,
38+
[
39+
'content-type' => 'text/html'
40+
],
41+
Utils::streamFor(json_encode(['foo' => 'bar']))
42+
);
43+
44+
$response = new Response($httpResponse);
45+
46+
$this->assertEquals('{"foo":"bar"}', $response['content']);
47+
}
48+
49+
public function testConstructResponseWith4xxHttpCode(): void
50+
{
51+
$this->expectException(InvalidArgumentException::class);
52+
$this->expectExceptionMessage('Invalid argument exception');
53+
$this->expectExceptionCode(418);
54+
$httpResponse = new HttpResponse(
55+
418,
56+
[
57+
'content-type' => 'application/json'
58+
],
59+
Utils::streamFor(json_encode(['foo' => 'bar']))
60+
);
61+
62+
new Response($httpResponse);
63+
}
64+
65+
public function testConstructResponseWith5xxHttpCode(): void
66+
{
67+
$this->expectException(ServerException::class);
68+
$this->expectExceptionMessage('Server Exception');
69+
$this->expectExceptionCode(510);
70+
$httpResponse = new HttpResponse(
71+
510,
72+
[],
73+
Utils::streamFor(json_encode(['foo' => 'bar']))
74+
);
75+
76+
new Response($httpResponse);
77+
}
78+
}

0 commit comments

Comments
 (0)