Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG-1.0.md
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions spec/Client/HttpExceptionHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/Client/HttpExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Exception/RedirectionHttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Akeneo\Pim\ApiClient\Exception;

/**
* Exception thrown when the HTTP response is a redirection (3xx).
*
* @author Laurent Petard <laurent.petard@akeneo.com>
* @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
{
}