Skip to content

Commit 7727489

Browse files
joelwurtzdbu
authored andcommitted
Add an option to only throw exception on 5XX error codes (#100)
Add an option to only throw exception on 5XX error codes
1 parent 6dee34d commit 7727489

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 1.8 (unreleased)
44

5+
### Added
6+
7+
- Add an option on ErrorPlugin to only throw exception on response with 5XX status code.
8+
59
### Changed
610

711
- AddPathPlugin no longer add prefix multiple times if a request is restarted - it now only adds the prefix if that request chain has not yet passed through the AddPathPlugin

spec/Plugin/ErrorPluginSpec.php

+16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ function it_throw_client_error_exception_on_4xx_error(RequestInterface $request,
3636
$promise->shouldThrow('Http\Client\Common\Exception\ClientErrorException')->duringWait();
3737
}
3838

39+
function it_does_not_throw_client_error_exception_on_4xx_error_if_only_server_exception(RequestInterface $request, ResponseInterface $response)
40+
{
41+
$this->beConstructedWith(['only_server_exception' => true]);
42+
43+
$response->getStatusCode()->willReturn('400');
44+
$response->getReasonPhrase()->willReturn('Bad request');
45+
46+
$next = function (RequestInterface $receivedRequest) use($request, $response) {
47+
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
48+
return new HttpFulfilledPromise($response->getWrappedObject());
49+
}
50+
};
51+
52+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
53+
}
54+
3955
function it_throw_server_error_exception_on_5xx_error(RequestInterface $request, ResponseInterface $response)
4056
{
4157
$response->getStatusCode()->willReturn('500');

src/Plugin/ErrorPlugin.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Http\Client\Common\Plugin;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
10+
use Symfony\Component\OptionsResolver\OptionsResolver;
1011

1112
/**
1213
* Throw exception when the response of a request is not acceptable.
@@ -17,6 +18,31 @@
1718
*/
1819
final class ErrorPlugin implements Plugin
1920
{
21+
/**
22+
* @var bool Whether this plugin should only throw 5XX Exceptions (default to false).
23+
*
24+
* If set to true 4XX Responses code will never throw an exception
25+
*/
26+
private $onlyServerException;
27+
28+
/**
29+
* @param array $config {
30+
*
31+
* @var bool only_server_exception Whether this plugin should only throw 5XX Exceptions (default to false).
32+
* }
33+
*/
34+
public function __construct(array $config = [])
35+
{
36+
$resolver = new OptionsResolver();
37+
$resolver->setDefaults([
38+
'only_server_exception' => false,
39+
]);
40+
$resolver->setAllowedTypes('only_server_exception', 'bool');
41+
$options = $resolver->resolve($config);
42+
43+
$this->onlyServerException = $options['only_server_exception'];
44+
}
45+
2046
/**
2147
* {@inheritdoc}
2248
*/
@@ -42,7 +68,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
4268
*/
4369
protected function transformResponseToException(RequestInterface $request, ResponseInterface $response)
4470
{
45-
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
71+
if (!$this->onlyServerException && $response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
4672
throw new ClientErrorException($response->getReasonPhrase(), $request, $response);
4773
}
4874

0 commit comments

Comments
 (0)