diff --git a/CHANGELOG-1.0.md b/CHANGELOG-1.0.md new file mode 100644 index 00000000..4ae13ff5 --- /dev/null +++ b/CHANGELOG-1.0.md @@ -0,0 +1,5 @@ +# 2.0.1 (2018-04-05) + +## Improvements + +- API-592: Handle error when the response is a redirection (https://github.com/akeneo/api-php-client/issues/72) diff --git a/spec/Client/HttpExceptionHandlerSpec.php b/spec/Client/HttpExceptionHandlerSpec.php index 87906632..9df0e502 100644 --- a/spec/Client/HttpExceptionHandlerSpec.php +++ b/spec/Client/HttpExceptionHandlerSpec.php @@ -5,6 +5,7 @@ use Akeneo\Pim\ApiClient\Exception\BadRequestHttpException; use Akeneo\Pim\ApiClient\Exception\ClientErrorHttpException; use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException; +use Akeneo\Pim\ApiClient\Exception\RedirectionHttpException; use Akeneo\Pim\ApiClient\Exception\ServerErrorHttpException; use Akeneo\Pim\ApiClient\Exception\UnauthorizedHttpException; use Akeneo\Pim\ApiClient\Exception\UnprocessableEntityHttpException; @@ -21,6 +22,26 @@ function it_is_initializable() $this->shouldHaveType(HttpExceptionHandler::class); } + function it_throws_redirection_exception_when_status_code_3xx( + RequestInterface $request, + ResponseInterface $response, + StreamInterface $responseBody + ) { + $response->getStatusCode()->willReturn(301); + $response->getBody()->willReturn($responseBody); + $responseBody->getContents()->willReturn('{"code": 301, "message": "Moved Permanently"}'); + $responseBody->rewind()->shouldBeCalled(); + $this + ->shouldThrow( + new RedirectionHttpException( + 'Moved Permanently', + $request->getWrappedObject(), + $response->getWrappedObject() + ) + ) + ->during('transformResponseToException', [$request, $response]); + } + function it_throws_bad_request_exception_when_status_code_400( RequestInterface $request, ResponseInterface $response, diff --git a/src/Client/HttpExceptionHandler.php b/src/Client/HttpExceptionHandler.php index 113ec81f..6dc0345e 100644 --- a/src/Client/HttpExceptionHandler.php +++ b/src/Client/HttpExceptionHandler.php @@ -5,6 +5,7 @@ use Akeneo\Pim\ApiClient\Exception\BadRequestHttpException; use Akeneo\Pim\ApiClient\Exception\ClientErrorHttpException; use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException; +use Akeneo\Pim\ApiClient\Exception\RedirectionHttpException; use Akeneo\Pim\ApiClient\Exception\ServerErrorHttpException; use Akeneo\Pim\ApiClient\Exception\UnauthorizedHttpException; use Akeneo\Pim\ApiClient\Exception\UnprocessableEntityHttpException; @@ -37,6 +38,10 @@ class HttpExceptionHandler */ public function transformResponseToException(RequestInterface $request, ResponseInterface $response) { + if ($response->getStatusCode() >= 300 && $response->getStatusCode() < 400) { + throw new RedirectionHttpException($this->getResponseMessage($response), $request, $response); + } + if (400 === $response->getStatusCode()) { throw new BadRequestHttpException($this->getResponseMessage($response), $request, $response); } diff --git a/src/Exception/RedirectionHttpException.php b/src/Exception/RedirectionHttpException.php new file mode 100644 index 00000000..5c7303ac --- /dev/null +++ b/src/Exception/RedirectionHttpException.php @@ -0,0 +1,14 @@ + + * @copyright 2018 Akeneo SAS (http://www.akeneo.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ +class RedirectionHttpException extends ClientErrorHttpException +{ +}