Skip to content

Commit 827a4a7

Browse files
committed
feedbacks
1 parent e711808 commit 827a4a7

File tree

7 files changed

+39
-34
lines changed

7 files changed

+39
-34
lines changed

src/platform/src/Bridge/Anthropic/ResultConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Anthropic;
1313

14-
use Symfony\AI\Platform\Exception\RateExceededException;
14+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1515
use Symfony\AI\Platform\Exception\RuntimeException;
1616
use Symfony\AI\Platform\Model;
1717
use Symfony\AI\Platform\Result\RawHttpResult;
@@ -44,7 +44,7 @@ public function convert(RawHttpResult|RawResultInterface $result, array $options
4444
if (429 === $response->getStatusCode()) {
4545
$retryAfter = $response->getHeaders(false)['retry-after'][0] ?? null;
4646
$retryAfterValue = $retryAfter ? (float) $retryAfter : null;
47-
throw new RateExceededException($retryAfterValue);
47+
throw new RateLimitExceededException($retryAfterValue);
4848
}
4949

5050
if ($options['stream'] ?? false) {

src/platform/src/Bridge/Gemini/Gemini/ResultConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\AI\Platform\Bridge\Gemini\Gemini;
1313

1414
use Symfony\AI\Platform\Bridge\Gemini\Gemini;
15-
use Symfony\AI\Platform\Exception\RateExceededException;
15+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1616
use Symfony\AI\Platform\Exception\RuntimeException;
1717
use Symfony\AI\Platform\Model;
1818
use Symfony\AI\Platform\Result\ChoiceResult;
@@ -46,7 +46,7 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
4646
$response = $result->getObject();
4747

4848
if (429 === $response->getStatusCode()) {
49-
throw new RateExceededException();
49+
throw new RateLimitExceededException();
5050
}
5151

5252
if ($options['stream'] ?? false) {

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

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

1414
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
1515
use Symfony\AI\Platform\Exception\ContentFilterException;
16-
use Symfony\AI\Platform\Exception\RateExceededException;
16+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1717
use Symfony\AI\Platform\Exception\RuntimeException;
1818
use Symfony\AI\Platform\Model;
1919
use Symfony\AI\Platform\Result\ChoiceResult;
@@ -50,12 +50,12 @@ public function convert(RawResultInterface|RawHttpResult $result, array $options
5050
$resetTime = null;
5151

5252
if (isset($headers['x-ratelimit-reset-requests'][0])) {
53-
$resetTime = $this->parseResetTime($headers['x-ratelimit-reset-requests'][0]);
53+
$resetTime = self::parseResetTime($headers['x-ratelimit-reset-requests'][0]);
5454
} elseif (isset($headers['x-ratelimit-reset-tokens'][0])) {
55-
$resetTime = $this->parseResetTime($headers['x-ratelimit-reset-tokens'][0]);
55+
$resetTime = self::parseResetTime($headers['x-ratelimit-reset-tokens'][0]);
5656
}
5757

58-
throw new RateExceededException($resetTime);
58+
throw new RateLimitExceededException($resetTime);
5959
}
6060

6161
if ($options['stream'] ?? false) {
@@ -211,7 +211,7 @@ private function convertToolCall(array $toolCall): ToolCall
211211
* - "6m0s"
212212
* - "2m30s"
213213
*/
214-
private function parseResetTime(string $resetTime): float
214+
private static function parseResetTime(string $resetTime): float
215215
{
216216
$seconds = 0;
217217

src/platform/src/Exception/RateExceededException.php renamed to src/platform/src/Exception/RateLimitExceededException.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414
/**
1515
* @author Floran Pagliai <floran.pagliai@gmail.com>
1616
*/
17-
final class RateExceededException extends \RuntimeException implements ExceptionInterface
17+
final class RateLimitExceededException extends \RuntimeException implements ExceptionInterface
1818
{
1919
public function __construct(
20-
public readonly ?float $retryAfter = null,
20+
private readonly ?float $retryAfter = null,
2121
) {
2222
parent::__construct('Rate limit exceeded.');
2323
}
24+
25+
public function getRetryAfter(): ?float
26+
{
27+
return $this->retryAfter;
28+
}
2429
}

src/platform/tests/Bridge/Anthropic/ResultConverterRateLimitTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPUnit\Framework\Attributes\Small;
1616
use PHPUnit\Framework\TestCase;
1717
use Symfony\AI\Platform\Bridge\Anthropic\ResultConverter;
18-
use Symfony\AI\Platform\Exception\RateExceededException;
18+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1919
use Symfony\AI\Platform\Result\RawHttpResult;
2020
use Symfony\Component\HttpClient\MockHttpClient;
2121
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -24,7 +24,7 @@
2424
#[Small]
2525
final class ResultConverterRateLimitTest extends TestCase
2626
{
27-
public function testRateLimitExceededThrowsException()
27+
public function testRateLimitExceededThrowsException(): void
2828
{
2929
$httpClient = new MockHttpClient([
3030
new MockResponse('{"type":"error","error":{"type":"rate_limit_error","message":"This request would exceed the rate limit for your organization"}}', [
@@ -38,18 +38,18 @@ public function testRateLimitExceededThrowsException()
3838
$httpResponse = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages');
3939
$handler = new ResultConverter();
4040

41-
$this->expectException(RateExceededException::class);
41+
$this->expectException(RateLimitExceededException::class);
4242
$this->expectExceptionMessage('Rate limit exceeded');
4343

4444
try {
4545
$handler->convert(new RawHttpResult($httpResponse));
46-
} catch (RateExceededException $e) {
47-
$this->assertSame(60.0, $e->retryAfter);
46+
} catch (RateLimitExceededException $e) {
47+
$this->assertSame(60.0, $e->getRetryAfter());
4848
throw $e;
4949
}
5050
}
5151

52-
public function testRateLimitExceededWithoutRetryAfter()
52+
public function testRateLimitExceededWithoutRetryAfter(): void
5353
{
5454
$httpClient = new MockHttpClient([
5555
new MockResponse('{"type":"error","error":{"type":"rate_limit_error","message":"This request would exceed the rate limit for your organization"}}', [
@@ -60,13 +60,13 @@ public function testRateLimitExceededWithoutRetryAfter()
6060
$httpResponse = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages');
6161
$handler = new ResultConverter();
6262

63-
$this->expectException(RateExceededException::class);
63+
$this->expectException(RateLimitExceededException::class);
6464
$this->expectExceptionMessage('Rate limit exceeded');
6565

6666
try {
6767
$handler->convert(new RawHttpResult($httpResponse));
68-
} catch (RateExceededException $e) {
69-
$this->assertNull($e->retryAfter);
68+
} catch (RateLimitExceededException $e) {
69+
$this->assertNull($e->getRetryAfter());
7070
throw $e;
7171
}
7272
}

src/platform/tests/Bridge/Gemini/Gemini/ResultConverterRateLimitTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPUnit\Framework\Attributes\Small;
1616
use PHPUnit\Framework\TestCase;
1717
use Symfony\AI\Platform\Bridge\Gemini\Gemini\ResultConverter;
18-
use Symfony\AI\Platform\Exception\RateExceededException;
18+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1919
use Symfony\AI\Platform\Result\RawHttpResult;
2020
use Symfony\Component\HttpClient\MockHttpClient;
2121
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -24,7 +24,7 @@
2424
#[Small]
2525
final class ResultConverterRateLimitTest extends TestCase
2626
{
27-
public function testRateLimitExceededThrowsException()
27+
public function testRateLimitExceededThrowsException(): void
2828
{
2929
$httpClient = new MockHttpClient([
3030
new MockResponse('{"error":{"code":429,"message":"Resource has been exhausted (e.g. check quota).","status":"RESOURCE_EXHAUSTED"}}', [
@@ -35,13 +35,13 @@ public function testRateLimitExceededThrowsException()
3535
$httpResponse = $httpClient->request('POST', 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent');
3636
$handler = new ResultConverter();
3737

38-
$this->expectException(RateExceededException::class);
38+
$this->expectException(RateLimitExceededException::class);
3939
$this->expectExceptionMessage('Rate limit exceeded.');
4040

4141
try {
4242
$handler->convert(new RawHttpResult($httpResponse));
43-
} catch (RateExceededException $e) {
44-
$this->assertNull($e->retryAfter);
43+
} catch (RateLimitExceededException $e) {
44+
$this->assertNull($e->getRetryAfter());
4545
throw $e;
4646
}
4747
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPUnit\Framework\Attributes\Small;
1616
use PHPUnit\Framework\TestCase;
1717
use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter;
18-
use Symfony\AI\Platform\Exception\RateExceededException;
18+
use Symfony\AI\Platform\Exception\RateLimitExceededException;
1919
use Symfony\AI\Platform\Result\RawHttpResult;
2020
use Symfony\Component\HttpClient\MockHttpClient;
2121
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -24,7 +24,7 @@
2424
#[Small]
2525
final class ResultConverterRateLimitTest extends TestCase
2626
{
27-
public function testRateLimitExceededWithRequestsResetTime()
27+
public function testRateLimitExceededWithRequestsResetTime(): void
2828
{
2929
$httpClient = new MockHttpClient([
3030
new MockResponse('{"error":{"message":"Rate limit reached for requests","type":"rate_limit_error"}}', [
@@ -40,18 +40,18 @@ public function testRateLimitExceededWithRequestsResetTime()
4040
$httpResponse = $httpClient->request('POST', 'https://api.openai.com/v1/chat/completions');
4141
$handler = new ResultConverter();
4242

43-
$this->expectException(RateExceededException::class);
43+
$this->expectException(RateLimitExceededException::class);
4444
$this->expectExceptionMessage('Rate limit exceeded.');
4545

4646
try {
4747
$handler->convert(new RawHttpResult($httpResponse));
48-
} catch (RateExceededException $e) {
49-
$this->assertSame(20.0, $e->retryAfter);
48+
} catch (RateLimitExceededException $e) {
49+
$this->assertSame(20.0, $e->getRetryAfter());
5050
throw $e;
5151
}
5252
}
5353

54-
public function testRateLimitExceededWithTokensResetTime()
54+
public function testRateLimitExceededWithTokensResetTime(): void
5555
{
5656
$httpClient = new MockHttpClient([
5757
new MockResponse('{"error":{"message":"Rate limit reached for tokens","type":"rate_limit_error"}}', [
@@ -67,13 +67,13 @@ public function testRateLimitExceededWithTokensResetTime()
6767
$httpResponse = $httpClient->request('POST', 'https://api.openai.com/v1/chat/completions');
6868
$handler = new ResultConverter();
6969

70-
$this->expectException(RateExceededException::class);
70+
$this->expectException(RateLimitExceededException::class);
7171
$this->expectExceptionMessage('Rate limit exceeded.');
7272

7373
try {
7474
$handler->convert(new RawHttpResult($httpResponse));
75-
} catch (RateExceededException $e) {
76-
$this->assertSame(150.0, $e->retryAfter);
75+
} catch (RateLimitExceededException $e) {
76+
$this->assertSame(150.0, $e->getRetryAfter());
7777
throw $e;
7878
}
7979
}

0 commit comments

Comments
 (0)