Skip to content

Commit

Permalink
Add an option to only throw exception on 5XX error codes (#100)
Browse files Browse the repository at this point in the history
Add an option to only throw exception on 5XX error codes
  • Loading branch information
joelwurtz authored and dbu committed May 28, 2018
1 parent 6dee34d commit 7727489
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 1.8 (unreleased)

### Added

- Add an option on ErrorPlugin to only throw exception on response with 5XX status code.

### Changed

- 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
Expand Down
16 changes: 16 additions & 0 deletions spec/Plugin/ErrorPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ function it_throw_client_error_exception_on_4xx_error(RequestInterface $request,
$promise->shouldThrow('Http\Client\Common\Exception\ClientErrorException')->duringWait();
}

function it_does_not_throw_client_error_exception_on_4xx_error_if_only_server_exception(RequestInterface $request, ResponseInterface $response)
{
$this->beConstructedWith(['only_server_exception' => true]);

$response->getStatusCode()->willReturn('400');
$response->getReasonPhrase()->willReturn('Bad request');

$next = function (RequestInterface $receivedRequest) use($request, $response) {
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
return new HttpFulfilledPromise($response->getWrappedObject());
}
};

$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
}

function it_throw_server_error_exception_on_5xx_error(RequestInterface $request, ResponseInterface $response)
{
$response->getStatusCode()->willReturn('500');
Expand Down
28 changes: 27 additions & 1 deletion src/Plugin/ErrorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Http\Client\Common\Plugin;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Throw exception when the response of a request is not acceptable.
Expand All @@ -17,6 +18,31 @@
*/
final class ErrorPlugin implements Plugin
{
/**
* @var bool Whether this plugin should only throw 5XX Exceptions (default to false).
*
* If set to true 4XX Responses code will never throw an exception
*/
private $onlyServerException;

/**
* @param array $config {
*
* @var bool only_server_exception Whether this plugin should only throw 5XX Exceptions (default to false).
* }
*/
public function __construct(array $config = [])
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'only_server_exception' => false,
]);
$resolver->setAllowedTypes('only_server_exception', 'bool');
$options = $resolver->resolve($config);

$this->onlyServerException = $options['only_server_exception'];
}

/**
* {@inheritdoc}
*/
Expand All @@ -42,7 +68,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
*/
protected function transformResponseToException(RequestInterface $request, ResponseInterface $response)
{
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
if (!$this->onlyServerException && $response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
throw new ClientErrorException($response->getReasonPhrase(), $request, $response);
}

Expand Down

0 comments on commit 7727489

Please sign in to comment.