Skip to content

Commit fbe72c2

Browse files
fix(platform): Add generic exceptions
- Fixes tests
1 parent e1451fe commit fbe72c2

File tree

2 files changed

+56
-45
lines changed

2 files changed

+56
-45
lines changed

src/platform/src/Exception/HttpErrorHandler.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,26 @@ public static function handleHttpError(ResponseInterface $response): void
3838

3939
private static function extractErrorMessage(ResponseInterface $response): string
4040
{
41-
try {
42-
$content = $response->getContent(false);
41+
$content = $response->getContent(false);
4342

44-
if ('' === $content) {
45-
return \sprintf('HTTP %d error', $response->getStatusCode());
46-
}
47-
48-
$data = json_decode($content, true);
49-
50-
if (null === $data || !\is_array($data)) {
51-
return \sprintf('HTTP %d error', $response->getStatusCode());
52-
}
53-
54-
if (isset($data['error']['message'])) {
55-
return $data['error']['message'];
56-
}
43+
if ('' === $content) {
44+
return \sprintf('HTTP %d error', $response->getStatusCode());
45+
}
5746

58-
if (isset($data['detail'])) {
59-
return $data['detail'];
60-
}
47+
$data = json_decode($content, true, 512, \JSON_THROW_ON_ERROR);
6148

49+
if (!\is_array($data)) {
6250
return $content;
63-
} catch (\Throwable) {
64-
try {
65-
$content = $response->getContent(false);
51+
}
52+
53+
if (isset($data['error']['message'])) {
54+
return $data['error']['message'];
55+
}
6656

67-
return !empty($content) ? $content : \sprintf('HTTP %d error', $response->getStatusCode());
68-
} catch (\Throwable) {
69-
return \sprintf('HTTP %d error', $response->getStatusCode());
70-
}
57+
if (isset($data['error']) && \is_string($data['error'])) {
58+
return $data['error'];
7159
}
60+
61+
return $data['message'] ?? $data['detail'] ?? $content;
7262
}
7363
}

src/platform/tests/Exception/HttpErrorHandlerTest.php

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,118 +18,139 @@
1818
use Symfony\AI\Platform\Exception\NotFoundException;
1919
use Symfony\AI\Platform\Exception\RuntimeException;
2020
use Symfony\AI\Platform\Exception\ServiceUnavailableException;
21+
use Symfony\Component\HttpClient\MockHttpClient;
2122
use Symfony\Component\HttpClient\Response\MockResponse;
2223

2324
#[CoversClass(HttpErrorHandler::class)]
2425
class HttpErrorHandlerTest extends TestCase
2526
{
26-
public function testHandleHttpErrorWithSuccessfulResponse():
27+
public function testHandleHttpErrorWithSuccessfulResponse()
2728
{
28-
$response = new MockResponse('{"success": true}', ['http_code' => 200]);
29+
$mockResponse = new MockResponse('{"success": true}', ['http_code' => 200]);
30+
$client = new MockHttpClient($mockResponse);
31+
$response = $client->request('GET', 'https://example.com');
2932

3033
$this->expectNotToPerformAssertions();
3134
HttpErrorHandler::handleHttpError($response);
3235
}
3336

34-
public function testHandleAuthenticationError():
37+
public function testHandleAuthenticationError()
3538
{
36-
$response = new MockResponse(
39+
$mockResponse = new MockResponse(
3740
'{"error": {"message": "Invalid API key"}}',
3841
['http_code' => 401]
3942
);
43+
$client = new MockHttpClient($mockResponse);
44+
$response = $client->request('GET', 'https://example.com');
4045

4146
$this->expectException(AuthenticationException::class);
4247
$this->expectExceptionMessage('Invalid API key');
4348
HttpErrorHandler::handleHttpError($response);
4449
}
4550

46-
public function testHandleNotFoundError():
51+
public function testHandleNotFoundError()
4752
{
48-
$response = new MockResponse(
53+
$mockResponse = new MockResponse(
4954
'{"error": {"message": "Model not found"}}',
5055
['http_code' => 404]
5156
);
57+
$client = new MockHttpClient($mockResponse);
58+
$response = $client->request('GET', 'https://example.com');
5259

5360
$this->expectException(NotFoundException::class);
5461
$this->expectExceptionMessage('Model not found');
5562
HttpErrorHandler::handleHttpError($response);
5663
}
5764

58-
public function testHandleServiceUnavailableError():
65+
public function testHandleServiceUnavailableError()
5966
{
60-
$response = new MockResponse(
67+
$mockResponse = new MockResponse(
6168
'{"error": {"message": "Service temporarily unavailable"}}',
6269
['http_code' => 503]
6370
);
71+
$client = new MockHttpClient($mockResponse);
72+
$response = $client->request('GET', 'https://example.com');
6473

6574
$this->expectException(ServiceUnavailableException::class);
6675
$this->expectExceptionMessage('Service temporarily unavailable');
6776
HttpErrorHandler::handleHttpError($response);
6877
}
6978

70-
public function testHandleGenericClientError():
79+
public function testHandleGenericClientError()
7180
{
72-
$response = new MockResponse(
81+
$mockResponse = new MockResponse(
7382
'{"error": {"message": "Bad request"}}',
7483
['http_code' => 400]
7584
);
85+
$client = new MockHttpClient($mockResponse);
86+
$response = $client->request('GET', 'https://example.com');
7687

7788
$this->expectException(RuntimeException::class);
7889
$this->expectExceptionMessage('HTTP 400: Bad request');
7990
HttpErrorHandler::handleHttpError($response);
8091
}
8192

82-
public function testHandleErrorWithDifferentMessageFormats():
93+
public function testHandleErrorWithDifferentMessageFormats()
8394
{
84-
$response = new MockResponse(
95+
$mockResponse = new MockResponse(
8596
'{"error": "Direct error message"}',
8697
['http_code' => 400]
8798
);
99+
$client = new MockHttpClient($mockResponse);
100+
$response = $client->request('GET', 'https://example.com');
88101

89102
$this->expectException(RuntimeException::class);
90103
$this->expectExceptionMessage('HTTP 400: Direct error message');
91104
HttpErrorHandler::handleHttpError($response);
92105
}
93106

94-
public function testHandleErrorWithMessageField():
107+
public function testHandleErrorWithMessageField()
95108
{
96-
$response = new MockResponse(
109+
$mockResponse = new MockResponse(
97110
'{"message": "Simple message format"}',
98111
['http_code' => 400]
99112
);
113+
$client = new MockHttpClient($mockResponse);
114+
$response = $client->request('GET', 'https://example.com');
100115

101116
$this->expectException(RuntimeException::class);
102117
$this->expectExceptionMessage('HTTP 400: Simple message format');
103118
HttpErrorHandler::handleHttpError($response);
104119
}
105120

106-
public function testHandleErrorWithDetailField():
121+
public function testHandleErrorWithDetailField()
107122
{
108-
$response = new MockResponse(
123+
$mockResponse = new MockResponse(
109124
'{"detail": "Detailed error information"}',
110125
['http_code' => 400]
111126
);
127+
$client = new MockHttpClient($mockResponse);
128+
$response = $client->request('GET', 'https://example.com');
112129

113130
$this->expectException(RuntimeException::class);
114131
$this->expectExceptionMessage('HTTP 400: Detailed error information');
115132
HttpErrorHandler::handleHttpError($response);
116133
}
117134

118-
public function testHandleErrorWithInvalidJson():
135+
public function testHandleErrorWithInvalidJson()
119136
{
120-
$response = new MockResponse(
137+
$mockResponse = new MockResponse(
121138
'Plain text error message',
122139
['http_code' => 500]
123140
);
141+
$client = new MockHttpClient($mockResponse);
142+
$response = $client->request('GET', 'https://example.com');
124143

125144
$this->expectException(RuntimeException::class);
126145
$this->expectExceptionMessage('HTTP 500: Plain text error message');
127146
HttpErrorHandler::handleHttpError($response);
128147
}
129148

130-
public function testHandleErrorWithEmptyResponse():
149+
public function testHandleErrorWithEmptyResponse()
131150
{
132-
$response = new MockResponse('', ['http_code' => 500]);
151+
$mockResponse = new MockResponse('', ['http_code' => 500]);
152+
$client = new MockHttpClient($mockResponse);
153+
$response = $client->request('GET', 'https://example.com');
133154

134155
$this->expectException(RuntimeException::class);
135156
$this->expectExceptionMessage('HTTP 500: HTTP 500 error');

0 commit comments

Comments
 (0)