Skip to content

Commit af7d865

Browse files
committed
feat: Handle NotAcceptableHttpException
1 parent 1eee552 commit af7d865

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

spec/Client/HttpExceptionHandlerSpec.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Akeneo\Pim\ApiClient\Exception\ClientErrorHttpException;
77
use Akeneo\Pim\ApiClient\Exception\ForbiddenHttpException;
88
use Akeneo\Pim\ApiClient\Exception\MethodNotAllowedHttpException;
9+
use Akeneo\Pim\ApiClient\Exception\NotAcceptableHttpException;
910
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException;
1011
use Akeneo\Pim\ApiClient\Exception\RedirectionHttpException;
1112
use Akeneo\Pim\ApiClient\Exception\ServerErrorHttpException;
@@ -149,6 +150,31 @@ function it_throws_method_not_allowed_exception_when_status_code_405(
149150
->during('transformResponseToException', [$request, $response]);
150151
}
151152

153+
function it_throws_method_not_allowed_exception_when_status_code_406(
154+
RequestInterface $request,
155+
ResponseInterface $response,
156+
StreamInterface $responseBody
157+
) {
158+
$response->getStatusCode()->willReturn(406);
159+
$response->getBody()->willReturn($responseBody);
160+
$responseBody->getContents()->willReturn(<<<JSON
161+
{
162+
"code": 406,
163+
"message": "‘xxx’ in ‘Accept‘ header is not valid. Only ‘application/json‘ is allowed."
164+
}
165+
JSON);
166+
$responseBody->rewind()->shouldBeCalled();
167+
$this
168+
->shouldThrow(
169+
new NotAcceptableHttpException(
170+
'‘xxx’ in ‘Accept‘ header is not valid. Only ‘application/json‘ is allowed.',
171+
$request->getWrappedObject(),
172+
$response->getWrappedObject()
173+
)
174+
)
175+
->during('transformResponseToException', [$request, $response]);
176+
}
177+
152178
function it_throws_bad_request_exception_when_status_code_422(
153179
RequestInterface $request,
154180
ResponseInterface $response,

src/Client/HttpExceptionHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Akeneo\Pim\ApiClient\Exception\ClientErrorHttpException;
77
use Akeneo\Pim\ApiClient\Exception\ForbiddenHttpException;
88
use Akeneo\Pim\ApiClient\Exception\MethodNotAllowedHttpException;
9+
use Akeneo\Pim\ApiClient\Exception\NotAcceptableHttpException;
910
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException;
1011
use Akeneo\Pim\ApiClient\Exception\RedirectionHttpException;
1112
use Akeneo\Pim\ApiClient\Exception\ServerErrorHttpException;
@@ -66,6 +67,10 @@ public function transformResponseToException(
6667
throw new MethodNotAllowedHttpException($this->getResponseMessage($response), $request, $response);
6768
}
6869

70+
if (HttpClient::HTTP_NOT_ACCEPTABLE === $response->getStatusCode()) {
71+
throw new NotAcceptableHttpException($this->getResponseMessage($response), $request, $response);
72+
}
73+
6974
if (HttpClient::HTTP_UNPROCESSABLE_ENTITY === $response->getStatusCode()) {
7075
throw new UnprocessableEntityHttpException($this->getResponseMessage($response), $request, $response);
7176
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Akeneo\Pim\ApiClient\Exception;
4+
5+
/**
6+
* @copyright 2022 Akeneo SAS (https://www.akeneo.com)
7+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8+
*/
9+
class NotAcceptableHttpException extends ClientErrorHttpException
10+
{
11+
protected function getAdditionalInformationMessage(): string
12+
{
13+
return '(see https://api.akeneo.com/php-client/exception.html#not-acceptable-exception)';
14+
}
15+
}

0 commit comments

Comments
 (0)