Skip to content

Commit 8c84477

Browse files
committed
Introduce BadRequestException to handle 400 Bad Request responses.
1 parent eeb757b commit 8c84477

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
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;
@@ -52,7 +53,7 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
5253

5354
if (400 === $response->getStatusCode()) {
5455
$errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request';
55-
throw new RuntimeException($errorMessage);
56+
throw new BadRequestException($errorMessage);
5657
}
5758

5859
if (429 === $response->getStatusCode()) {
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: 3 additions & 2 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;
@@ -197,7 +198,7 @@ public function testThrowsExceptionForUnsupportedFinishReason()
197198
$converter->convert(new RawHttpResult($httpResponse));
198199
}
199200

200-
public function testThrowsRuntimeExceptionOnBadRequest()
201+
public function testThrowsBadRequestExceptionOnBadRequestResponse()
201202
{
202203
$converter = new ResultConverter();
203204
$httpResponse = self::createMock(ResponseInterface::class);
@@ -208,7 +209,7 @@ public function testThrowsRuntimeExceptionOnBadRequest()
208209
],
209210
]));
210211

211-
$this->expectException(RuntimeException::class);
212+
$this->expectException(BadRequestException::class);
212213
$this->expectExceptionMessage('Bad Request: invalid parameters');
213214

214215
$converter->convert(new RawHttpResult($httpResponse));

0 commit comments

Comments
 (0)