diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e5e0a0..2059785 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 2.0 (unreleased) ### Changed +- HttpClientRouter now throws a HttpClientNoMatchException instead of a RequestException if it can not find a client for the request. - RetryPlugin will no longer retry requests when the response failed with a HTTP code < 500. - Abstract method `HttpClientPool::chooseHttpClient()` has now an explicit return type (`Http\Client\Common\HttpClientPoolItem`) - Interface method `Plugin::handleRequest(...)` has now an explicit return type (`Http\Promise\Promise`) diff --git a/spec/HttpClientRouterSpec.php b/spec/HttpClientRouterSpec.php index a409203..f90c034 100644 --- a/spec/HttpClientRouterSpec.php +++ b/spec/HttpClientRouterSpec.php @@ -2,6 +2,7 @@ namespace spec\Http\Client\Common; +use Http\Client\Common\Exception\HttpClientNoMatchException; use Http\Client\Common\HttpClientRouter; use Http\Message\RequestMatcher; use Http\Client\HttpAsyncClient; @@ -11,7 +12,6 @@ use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; use Http\Client\Common\HttpClientRouterInterface; -use Http\Client\Exception\RequestException; class HttpClientRouterSpec extends ObjectBehavior { @@ -58,7 +58,7 @@ public function it_throw_exception_on_send_request(RequestMatcher $matcher, Http $this->addClient($client, $matcher); $matcher->matches($request)->willReturn(false); - $this->shouldThrow(RequestException::class)->duringSendRequest($request); + $this->shouldThrow(HttpClientNoMatchException::class)->duringSendRequest($request); } public function it_throw_exception_on_send_async_request(RequestMatcher $matcher, HttpAsyncClient $client, RequestInterface $request) @@ -66,6 +66,6 @@ public function it_throw_exception_on_send_async_request(RequestMatcher $matcher $this->addClient($client, $matcher); $matcher->matches($request)->willReturn(false); - $this->shouldThrow(RequestException::class)->duringSendAsyncRequest($request); + $this->shouldThrow(HttpClientNoMatchException::class)->duringSendAsyncRequest($request); } } diff --git a/src/Exception/HttpClientNoMatchException.php b/src/Exception/HttpClientNoMatchException.php new file mode 100644 index 0000000..437467c --- /dev/null +++ b/src/Exception/HttpClientNoMatchException.php @@ -0,0 +1,30 @@ + + */ +final class HttpClientNoMatchException extends TransferException +{ + private $request; + + public function __construct(string $message, RequestInterface $request, \Exception $previous = null) + { + $this->request = $request; + + parent::__construct($message, 0, $previous); + } + + public function getRequest(): RequestInterface + { + return $this->request; + } +} diff --git a/src/HttpClientRouter.php b/src/HttpClientRouter.php index 37f8c3c..ccd4a1c 100644 --- a/src/HttpClientRouter.php +++ b/src/HttpClientRouter.php @@ -4,7 +4,7 @@ namespace Http\Client\Common; -use Http\Client\Exception\RequestException; +use Http\Client\Common\Exception\HttpClientNoMatchException; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; use Http\Message\RequestMatcher; @@ -66,6 +66,6 @@ private function chooseHttpClient(RequestInterface $request) } } - throw new RequestException('No client found for the specified request', $request); + throw new HttpClientNoMatchException('No client found for the specified request', $request); } }