From 88ea8971173433175a176b207d578ca06b4f5cb9 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Tue, 8 Oct 2019 23:41:00 +0200 Subject: [PATCH] Drop RequestException, use exception whitelist for retry --- .../UnprocessedRequestException.php | 8 +++----- src/Interceptor/RetryRequests.php | 15 +++++--------- src/InvalidRequestException.php | 15 +++++++++++++- src/RequestException.php | 20 ------------------- 4 files changed, 22 insertions(+), 36 deletions(-) delete mode 100644 src/RequestException.php diff --git a/src/Connection/UnprocessedRequestException.php b/src/Connection/UnprocessedRequestException.php index 03752c0e..15955a43 100644 --- a/src/Connection/UnprocessedRequestException.php +++ b/src/Connection/UnprocessedRequestException.php @@ -3,13 +3,11 @@ namespace Amp\Http\Client\Connection; use Amp\Http\Client\HttpException; -use Amp\Http\Client\Request; -use Amp\Http\Client\RequestException; -final class UnprocessedRequestException extends RequestException +final class UnprocessedRequestException extends HttpException { - public function __construct(Request $request, HttpException $previous) + public function __construct(HttpException $previous) { - parent::__construct($request, "The request was not processed and can be safely retried", 0, $previous); + parent::__construct("The request was not processed and can be safely retried", 0, $previous); } } diff --git a/src/Interceptor/RetryRequests.php b/src/Interceptor/RetryRequests.php index ad73b5ba..5b5cf00f 100644 --- a/src/Interceptor/RetryRequests.php +++ b/src/Interceptor/RetryRequests.php @@ -6,11 +6,8 @@ use Amp\Http\Client\ApplicationInterceptor; use Amp\Http\Client\Client; use Amp\Http\Client\Connection\UnprocessedRequestException; -use Amp\Http\Client\HttpException; -use Amp\Http\Client\InvalidRequestException; -use Amp\Http\Client\ParseException; use Amp\Http\Client\Request; -use Amp\Http\Client\TimeoutException; +use Amp\Http\Client\SocketException; use Amp\Promise; use function Amp\call; @@ -27,23 +24,21 @@ public function __construct(int $retryLimit) public function request(Request $request, CancellationToken $cancellation, Client $client): Promise { return call(function () use ($request, $cancellation, $client) { - $attempts = 1; + $attempt = 1; do { try { return yield $client->request(clone $request, $cancellation); } catch (UnprocessedRequestException $exception) { // Request was deemed retryable by connection, so carry on. - } catch (InvalidRequestException | ParseException | TimeoutException $exception) { - // Request is or response is invalid or request timed out, so do not retry. - throw $exception; - } catch (HttpException $exception) { + } catch (SocketException $exception) { if (!$request->isIdempotent()) { throw $exception; } + // Request can safely be retried. } - } while ($attempts++ <= $this->retryLimit); + } while ($attempt++ <= $this->retryLimit); throw $exception; }); diff --git a/src/InvalidRequestException.php b/src/InvalidRequestException.php index 389f40a4..f02570bb 100644 --- a/src/InvalidRequestException.php +++ b/src/InvalidRequestException.php @@ -2,6 +2,19 @@ namespace Amp\Http\Client; -final class InvalidRequestException extends RequestException +final class InvalidRequestException extends HttpException { + /** @var Request */ + private $request; + + public function __construct(Request $request, string $message, int $code = 0, \Throwable $previous = null) + { + parent::__construct($message, $code, $previous); + $this->request = $request; + } + + public function getRequest(): Request + { + return $this->request; + } } diff --git a/src/RequestException.php b/src/RequestException.php deleted file mode 100644 index 5d26b294..00000000 --- a/src/RequestException.php +++ /dev/null @@ -1,20 +0,0 @@ -request = $request; - } - - final public function getRequest(): Request - { - return $this->request; - } -}