Skip to content

Commit 953bbf5

Browse files
committed
Add an option to only throw exception on 5XX error codes
1 parent 7533c2d commit 953bbf5

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

spec/Plugin/ErrorPluginSpec.php

Lines changed: 16 additions & 0 deletions
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

Lines changed: 27 additions & 1 deletion
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)