Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include rate limit data in ApiLimitExceededException #327

Open
wants to merge 6 commits into
base: 4.9
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/Exception/ApiLimitExceededException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@

namespace DigitalOceanV2\Exception;

use DigitalOceanV2\Entity\RateLimit;

/**
* @author Graham Campbell <[email protected]>
*/
class ApiLimitExceededException extends RuntimeException
{
public ?RateLimit $rateLimit;

public function __construct(string $message, int $code, ?RateLimit $rateLimit = null)
{
parent::__construct($message, $code);

$this->rateLimit = $rateLimit;
}
}
17 changes: 11 additions & 6 deletions src/HttpClient/Plugin/ExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace DigitalOceanV2\HttpClient\Plugin;

use DigitalOceanV2\Entity\RateLimit;
use DigitalOceanV2\Exception\ApiLimitExceededException;
use DigitalOceanV2\Exception\ErrorException;
use DigitalOceanV2\Exception\ExceptionInterface;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
$status = $response->getStatusCode();

if ($status >= 400 && $status < 600) {
throw self::createException($status, ResponseMediator::getErrorMessage($response) ?? $response->getReasonPhrase());
throw self::createException($response);
}

return $response;
Expand All @@ -62,19 +63,23 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
/**
* Create an exception from a status code and error message.
*
* @param int $status
* @param string $message
*
* @return ErrorException|RuntimeException
*/
private static function createException(int $status, string $message): ExceptionInterface
private static function createException(ResponseInterface $response): ExceptionInterface
{
$status = $response->getStatusCode();
$message = ResponseMediator::getErrorMessage($response) ?? $response->getReasonPhrase();

if (400 === $status || 422 === $status) {
return new ValidationFailedException($message, $status);
}

if (429 === $status) {
return new ApiLimitExceededException($message, $status);
return new ApiLimitExceededException(
$message,
$status,
new RateLimit(ResponseMediator::getRateLimit($response))
);
}

if (404 === $status) {
Expand Down
69 changes: 69 additions & 0 deletions tests/HttpClient/Plugin/ExceptionThrowerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/*
* This file is part of the DigitalOcean API library.
*
* (c) Antoine Kirk <[email protected]>
* (c) Graham Campbell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DigitalOceanV2\Tests\HttpClient\Plugin;

use DigitalOceanV2\Entity\RateLimit;
use DigitalOceanV2\Exception\ApiLimitExceededException;
use DigitalOceanV2\HttpClient\Plugin\ExceptionThrower;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Http\Client\Promise\HttpFulfilledPromise;
use PHPUnit\Framework\TestCase;

/**
* @author Jon Cram <[email protected]>
*/
class ExceptionThrowerTest extends TestCase
{
public function testApiLimitExceededExceptionIncludesRateLimit(): void
{
$exceptionThrower = new ExceptionThrower();

$rateLimitReset = \rand();
$rateLimitRemaining = \rand();
$rateLimitLimit = \rand();

$request = new Request('GET', 'https://example.com/');
$response = new Response(
429,
[
'RateLimit-Reset' => $rateLimitReset,
'RateLimit-Remaining' => $rateLimitRemaining,
'RateLimit-Limit' => $rateLimitLimit,
]
);

$callable = function () use ($response) {
return new HttpFulfilledPromise($response);
};

$exception = null;

try {
$exceptionThrower->handleRequest($request, $callable, $callable);
} catch (ApiLimitExceededException $exception) {
}

self::assertInstanceOf(ApiLimitExceededException::class, $exception);

$expectedExceptionRateLimit = new RateLimit([
'reset' => $rateLimitReset,
'remaining' => $rateLimitRemaining,
'limit' => $rateLimitLimit,
]);

self::assertEquals($expectedExceptionRateLimit, $exception->rateLimit);
}
}
Loading