Skip to content
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

Add an option to only throw exception on 5XX error codes #100

Merged
merged 3 commits into from
May 28, 2018
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
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