Skip to content

Commit de6dfdf

Browse files
Nyholmdbu
authored andcommitted
Added HttpClientNoMatchException for HttpClientRouter (#153)
1 parent 7656a55 commit de6dfdf

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.0 (unreleased)
44

55
### Changed
6+
- HttpClientRouter now throws a HttpClientNoMatchException instead of a RequestException if it can not find a client for the request.
67
- RetryPlugin will no longer retry requests when the response failed with a HTTP code < 500.
78
- Abstract method `HttpClientPool::chooseHttpClient()` has now an explicit return type (`Http\Client\Common\HttpClientPoolItem`)
89
- Interface method `Plugin::handleRequest(...)` has now an explicit return type (`Http\Promise\Promise`)

spec/HttpClientRouterSpec.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace spec\Http\Client\Common;
44

5+
use Http\Client\Common\Exception\HttpClientNoMatchException;
56
use Http\Client\Common\HttpClientRouter;
67
use Http\Message\RequestMatcher;
78
use Http\Client\HttpAsyncClient;
@@ -11,7 +12,6 @@
1112
use Psr\Http\Message\ResponseInterface;
1213
use PhpSpec\ObjectBehavior;
1314
use Http\Client\Common\HttpClientRouterInterface;
14-
use Http\Client\Exception\RequestException;
1515

1616
class HttpClientRouterSpec extends ObjectBehavior
1717
{
@@ -58,14 +58,14 @@ public function it_throw_exception_on_send_request(RequestMatcher $matcher, Http
5858
$this->addClient($client, $matcher);
5959
$matcher->matches($request)->willReturn(false);
6060

61-
$this->shouldThrow(RequestException::class)->duringSendRequest($request);
61+
$this->shouldThrow(HttpClientNoMatchException::class)->duringSendRequest($request);
6262
}
6363

6464
public function it_throw_exception_on_send_async_request(RequestMatcher $matcher, HttpAsyncClient $client, RequestInterface $request)
6565
{
6666
$this->addClient($client, $matcher);
6767
$matcher->matches($request)->willReturn(false);
6868

69-
$this->shouldThrow(RequestException::class)->duringSendAsyncRequest($request);
69+
$this->shouldThrow(HttpClientNoMatchException::class)->duringSendAsyncRequest($request);
7070
}
7171
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Http\Client\Common\Exception;
6+
7+
use Http\Client\Exception\TransferException;
8+
use Psr\Http\Message\RequestInterface;
9+
10+
/**
11+
* Thrown when a http client match in the HTTPClientRouter.
12+
*
13+
* @author Tobias Nyholm <[email protected]>
14+
*/
15+
final class HttpClientNoMatchException extends TransferException
16+
{
17+
private $request;
18+
19+
public function __construct(string $message, RequestInterface $request, \Exception $previous = null)
20+
{
21+
$this->request = $request;
22+
23+
parent::__construct($message, 0, $previous);
24+
}
25+
26+
public function getRequest(): RequestInterface
27+
{
28+
return $this->request;
29+
}
30+
}

src/HttpClientRouter.php

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

55
namespace Http\Client\Common;
66

7-
use Http\Client\Exception\RequestException;
7+
use Http\Client\Common\Exception\HttpClientNoMatchException;
88
use Http\Client\HttpAsyncClient;
99
use Http\Client\HttpClient;
1010
use Http\Message\RequestMatcher;
@@ -66,6 +66,6 @@ private function chooseHttpClient(RequestInterface $request)
6666
}
6767
}
6868

69-
throw new RequestException('No client found for the specified request', $request);
69+
throw new HttpClientNoMatchException('No client found for the specified request', $request);
7070
}
7171
}

0 commit comments

Comments
 (0)