Skip to content

Added HttpClientNoMatchException for HttpClientRouter #153

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

Merged
merged 4 commits into from
Jan 4, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
6 changes: 3 additions & 3 deletions spec/HttpClientRouterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -58,14 +58,14 @@ 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)
{
$this->addClient($client, $matcher);
$matcher->matches($request)->willReturn(false);

$this->shouldThrow(RequestException::class)->duringSendAsyncRequest($request);
$this->shouldThrow(HttpClientNoMatchException::class)->duringSendAsyncRequest($request);
}
}
30 changes: 30 additions & 0 deletions src/Exception/HttpClientNoMatchException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Http\Client\Common\Exception;

use Http\Client\Exception\TransferException;
use Psr\Http\Message\RequestInterface;

/**
* Thrown when a http client match in the HTTPClientRouter.
*
* @author Tobias Nyholm <[email protected]>
*/
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;
}
}
4 changes: 2 additions & 2 deletions src/HttpClientRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}