Skip to content

Commit 95f1ad2

Browse files
committed
feat: Handle UnsupportedMediaTypeHttpException
1 parent af7d865 commit 95f1ad2

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
@@ -13,6 +13,7 @@
1313
use Akeneo\Pim\ApiClient\Exception\UnauthorizedHttpException;
1414
use Akeneo\Pim\ApiClient\Exception\UnprocessableEntityHttpException;
1515
use Akeneo\Pim\ApiClient\Client\HttpExceptionHandler;
16+
use Akeneo\Pim\ApiClient\Exception\UnsupportedMediaTypeHttpException;
1617
use PhpSpec\ObjectBehavior;
1718
use Psr\Http\Message\RequestInterface;
1819
use Psr\Http\Message\ResponseInterface;
@@ -175,6 +176,31 @@ function it_throws_method_not_allowed_exception_when_status_code_406(
175176
->during('transformResponseToException', [$request, $response]);
176177
}
177178

179+
function it_throws_method_not_allowed_exception_when_status_code_415(
180+
RequestInterface $request,
181+
ResponseInterface $response,
182+
StreamInterface $responseBody
183+
) {
184+
$response->getStatusCode()->willReturn(415);
185+
$response->getBody()->willReturn($responseBody);
186+
$responseBody->getContents()->willReturn(<<<JSON
187+
{
188+
"code": 415,
189+
"message": "The ‘Content-type’ header is missing. ‘application/json’ has to specified as value."
190+
}
191+
JSON);
192+
$responseBody->rewind()->shouldBeCalled();
193+
$this
194+
->shouldThrow(
195+
new UnsupportedMediaTypeHttpException(
196+
'The ‘Content-type’ header is missing. ‘application/json’ has to specified as value.',
197+
$request->getWrappedObject(),
198+
$response->getWrappedObject()
199+
)
200+
)
201+
->during('transformResponseToException', [$request, $response]);
202+
}
203+
178204
function it_throws_bad_request_exception_when_status_code_422(
179205
RequestInterface $request,
180206
ResponseInterface $response,

src/Client/HttpExceptionHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Akeneo\Pim\ApiClient\Exception\ServerErrorHttpException;
1313
use Akeneo\Pim\ApiClient\Exception\UnauthorizedHttpException;
1414
use Akeneo\Pim\ApiClient\Exception\UnprocessableEntityHttpException;
15+
use Akeneo\Pim\ApiClient\Exception\UnsupportedMediaTypeHttpException;
1516
use Psr\Http\Message\RequestInterface;
1617
use Psr\Http\Message\ResponseInterface;
1718

@@ -71,6 +72,10 @@ public function transformResponseToException(
7172
throw new NotAcceptableHttpException($this->getResponseMessage($response), $request, $response);
7273
}
7374

75+
if (HttpClient::HTTP_UNSUPPORTED_MEDIA_TYPE === $response->getStatusCode()) {
76+
throw new UnsupportedMediaTypeHttpException($this->getResponseMessage($response), $request, $response);
77+
}
78+
7479
if (HttpClient::HTTP_UNPROCESSABLE_ENTITY === $response->getStatusCode()) {
7580
throw new UnprocessableEntityHttpException($this->getResponseMessage($response), $request, $response);
7681
}
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 UnsupportedMediaTypeHttpException extends ClientErrorHttpException
10+
{
11+
protected function getAdditionalInformationMessage(): string
12+
{
13+
return '(see https://api.akeneo.com/php-client/exception.html#unsupported-media-type-exception)';
14+
}
15+
}

0 commit comments

Comments
 (0)