Skip to content

Commit 0c1e780

Browse files
oestevechr-hertel
authored andcommitted
[Platform][OpenAI] Improve error reporting for Bad Request
1 parent c9434fa commit 0c1e780

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
1515
use Symfony\AI\Platform\Exception\AuthenticationException;
16+
use Symfony\AI\Platform\Exception\BadRequestException;
1617
use Symfony\AI\Platform\Exception\ContentFilterException;
1718
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1819
use Symfony\AI\Platform\Exception\RuntimeException;
@@ -50,6 +51,11 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
5051
throw new AuthenticationException($errorMessage);
5152
}
5253

54+
if (400 === $response->getStatusCode()) {
55+
$errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request';
56+
throw new BadRequestException($errorMessage);
57+
}
58+
5359
if (429 === $response->getStatusCode()) {
5460
$headers = $response->getHeaders(false);
5561
$resetTime = null;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Exception;
13+
14+
/**
15+
* @author Oscar Esteve <[email protected]>
16+
*/
17+
class BadRequestException extends RuntimeException
18+
{
19+
}

src/platform/tests/Bridge/OpenAi/Gpt/ResultConverterTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter;
1616
use Symfony\AI\Platform\Exception\AuthenticationException;
17+
use Symfony\AI\Platform\Exception\BadRequestException;
1718
use Symfony\AI\Platform\Exception\ContentFilterException;
1819
use Symfony\AI\Platform\Exception\RuntimeException;
1920
use Symfony\AI\Platform\Result\ChoiceResult;
@@ -196,4 +197,33 @@ public function testThrowsExceptionForUnsupportedFinishReason()
196197

197198
$converter->convert(new RawHttpResult($httpResponse));
198199
}
200+
201+
public function testThrowsBadRequestExceptionOnBadRequestResponse()
202+
{
203+
$converter = new ResultConverter();
204+
$httpResponse = self::createMock(ResponseInterface::class);
205+
$httpResponse->method('getStatusCode')->willReturn(400);
206+
$httpResponse->method('getContent')->willReturn(json_encode([
207+
'error' => [
208+
'message' => 'Bad Request: invalid parameters',
209+
],
210+
]));
211+
212+
$this->expectException(BadRequestException::class);
213+
$this->expectExceptionMessage('Bad Request: invalid parameters');
214+
215+
$converter->convert(new RawHttpResult($httpResponse));
216+
}
217+
218+
public function testThrowsBadRequestExceptionOnBadRequestResponseWithNoResponseBody()
219+
{
220+
$converter = new ResultConverter();
221+
$httpResponse = self::createMock(ResponseInterface::class);
222+
$httpResponse->method('getStatusCode')->willReturn(400);
223+
224+
$this->expectException(BadRequestException::class);
225+
$this->expectExceptionMessage('Bad Request');
226+
227+
$converter->convert(new RawHttpResult($httpResponse));
228+
}
199229
}

0 commit comments

Comments
 (0)