Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/platform/src/Bridge/OpenAi/Gpt/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Exception\AuthenticationException;
use Symfony\AI\Platform\Exception\BadRequestException;
use Symfony\AI\Platform\Exception\ContentFilterException;
use Symfony\AI\Platform\Exception\RateLimitExceededException;
use Symfony\AI\Platform\Exception\RuntimeException;
Expand Down Expand Up @@ -50,6 +51,11 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
throw new AuthenticationException($errorMessage);
}

if (400 === $response->getStatusCode()) {
$errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request';
throw new BadRequestException($errorMessage);
}

if (429 === $response->getStatusCode()) {
$headers = $response->getHeaders(false);
$resetTime = null;
Expand Down
19 changes: 19 additions & 0 deletions src/platform/src/Exception/BadRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Exception;

/**
* @author Oscar Esteve <[email protected]>
*/
class BadRequestException extends RuntimeException
{
}
30 changes: 30 additions & 0 deletions src/platform/tests/Bridge/OpenAi/Gpt/ResultConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter;
use Symfony\AI\Platform\Exception\AuthenticationException;
use Symfony\AI\Platform\Exception\BadRequestException;
use Symfony\AI\Platform\Exception\ContentFilterException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\Result\ChoiceResult;
Expand Down Expand Up @@ -196,4 +197,33 @@ public function testThrowsExceptionForUnsupportedFinishReason()

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

public function testThrowsBadRequestExceptionOnBadRequestResponse()
{
$converter = new ResultConverter();
$httpResponse = self::createMock(ResponseInterface::class);
$httpResponse->method('getStatusCode')->willReturn(400);
$httpResponse->method('getContent')->willReturn(json_encode([
'error' => [
'message' => 'Bad Request: invalid parameters',
],
]));

$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Bad Request: invalid parameters');

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

public function testThrowsBadRequestExceptionOnBadRequestResponseWithNoResponseBody()
{
$converter = new ResultConverter();
$httpResponse = self::createMock(ResponseInterface::class);
$httpResponse->method('getStatusCode')->willReturn(400);

$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Bad Request');

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