Skip to content

Commit 46c4609

Browse files
Merge pull request #127 from akeneo/API-592
API-592: Handle error when the response is a redirection
2 parents 68a9021 + e4edd4c commit 46c4609

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

CHANGELOG-1.0.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 2.0.1 (2018-04-05)
2+
3+
## Improvements
4+
5+
- API-592: Handle error when the response is a redirection (https://github.com/akeneo/api-php-client/issues/72)

spec/Client/HttpExceptionHandlerSpec.php

+21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Akeneo\Pim\ApiClient\Exception\BadRequestHttpException;
66
use Akeneo\Pim\ApiClient\Exception\ClientErrorHttpException;
77
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException;
8+
use Akeneo\Pim\ApiClient\Exception\RedirectionHttpException;
89
use Akeneo\Pim\ApiClient\Exception\ServerErrorHttpException;
910
use Akeneo\Pim\ApiClient\Exception\UnauthorizedHttpException;
1011
use Akeneo\Pim\ApiClient\Exception\UnprocessableEntityHttpException;
@@ -21,6 +22,26 @@ function it_is_initializable()
2122
$this->shouldHaveType(HttpExceptionHandler::class);
2223
}
2324

25+
function it_throws_redirection_exception_when_status_code_3xx(
26+
RequestInterface $request,
27+
ResponseInterface $response,
28+
StreamInterface $responseBody
29+
) {
30+
$response->getStatusCode()->willReturn(301);
31+
$response->getBody()->willReturn($responseBody);
32+
$responseBody->getContents()->willReturn('{"code": 301, "message": "Moved Permanently"}');
33+
$responseBody->rewind()->shouldBeCalled();
34+
$this
35+
->shouldThrow(
36+
new RedirectionHttpException(
37+
'Moved Permanently',
38+
$request->getWrappedObject(),
39+
$response->getWrappedObject()
40+
)
41+
)
42+
->during('transformResponseToException', [$request, $response]);
43+
}
44+
2445
function it_throws_bad_request_exception_when_status_code_400(
2546
RequestInterface $request,
2647
ResponseInterface $response,

src/Client/HttpExceptionHandler.php

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Akeneo\Pim\ApiClient\Exception\BadRequestHttpException;
66
use Akeneo\Pim\ApiClient\Exception\ClientErrorHttpException;
77
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException;
8+
use Akeneo\Pim\ApiClient\Exception\RedirectionHttpException;
89
use Akeneo\Pim\ApiClient\Exception\ServerErrorHttpException;
910
use Akeneo\Pim\ApiClient\Exception\UnauthorizedHttpException;
1011
use Akeneo\Pim\ApiClient\Exception\UnprocessableEntityHttpException;
@@ -37,6 +38,10 @@ class HttpExceptionHandler
3738
*/
3839
public function transformResponseToException(RequestInterface $request, ResponseInterface $response)
3940
{
41+
if ($response->getStatusCode() >= 300 && $response->getStatusCode() < 400) {
42+
throw new RedirectionHttpException($this->getResponseMessage($response), $request, $response);
43+
}
44+
4045
if (400 === $response->getStatusCode()) {
4146
throw new BadRequestHttpException($this->getResponseMessage($response), $request, $response);
4247
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Akeneo\Pim\ApiClient\Exception;
4+
5+
/**
6+
* Exception thrown when the HTTP response is a redirection (3xx).
7+
*
8+
* @author Laurent Petard <[email protected]>
9+
* @copyright 2018 Akeneo SAS (http://www.akeneo.com)
10+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
11+
*/
12+
class RedirectionHttpException extends ClientErrorHttpException
13+
{
14+
}

0 commit comments

Comments
 (0)