Skip to content

Commit 1eee552

Browse files
committed
feat: Handle MethodNotAllowedException
1 parent d5f9486 commit 1eee552

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

spec/Client/HttpExceptionHandlerSpec.php

Lines changed: 28 additions & 2 deletions
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\ForbiddenHttpException;
8+
use Akeneo\Pim\ApiClient\Exception\MethodNotAllowedHttpException;
89
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException;
910
use Akeneo\Pim\ApiClient\Exception\RedirectionHttpException;
1011
use Akeneo\Pim\ApiClient\Exception\ServerErrorHttpException;
@@ -123,6 +124,31 @@ function it_throws_not_found_exception_when_status_code_404(
123124
->during('transformResponseToException', [$request, $response]);
124125
}
125126

127+
function it_throws_method_not_allowed_exception_when_status_code_405(
128+
RequestInterface $request,
129+
ResponseInterface $response,
130+
StreamInterface $responseBody
131+
) {
132+
$response->getStatusCode()->willReturn(405);
133+
$response->getBody()->willReturn($responseBody);
134+
$responseBody->getContents()->willReturn(<<<JSON
135+
{
136+
"code": 405,
137+
"message": "No route found for 'POST /api/rest/v1/products/myproduct': Method Not Allowed (Allow: GET, PATCH, DELETE)"
138+
}
139+
JSON);
140+
$responseBody->rewind()->shouldBeCalled();
141+
$this
142+
->shouldThrow(
143+
new MethodNotAllowedHttpException(
144+
'No route found for \'POST /api/rest/v1/products/myproduct\': Method Not Allowed (Allow: GET, PATCH, DELETE)',
145+
$request->getWrappedObject(),
146+
$response->getWrappedObject()
147+
)
148+
)
149+
->during('transformResponseToException', [$request, $response]);
150+
}
151+
126152
function it_throws_bad_request_exception_when_status_code_422(
127153
RequestInterface $request,
128154
ResponseInterface $response,
@@ -148,9 +174,9 @@ function it_throws_bad_request_exception_when_status_code_4xx(
148174
ResponseInterface $response,
149175
StreamInterface $responseBody
150176
) {
151-
$response->getStatusCode()->willReturn(405);
177+
$response->getStatusCode()->willReturn(418);
152178
$response->getBody()->willReturn($responseBody);
153-
$responseBody->getContents()->willReturn('{"code": 405, "message": "Not allowed."}');
179+
$responseBody->getContents()->willReturn('{"code": 418, "message": "Not allowed."}');
154180
$responseBody->rewind()->shouldBeCalled();
155181
$this
156182
->shouldThrow(

src/Client/HttpExceptionHandler.php

Lines changed: 5 additions & 0 deletions
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\ForbiddenHttpException;
8+
use Akeneo\Pim\ApiClient\Exception\MethodNotAllowedHttpException;
89
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException;
910
use Akeneo\Pim\ApiClient\Exception\RedirectionHttpException;
1011
use Akeneo\Pim\ApiClient\Exception\ServerErrorHttpException;
@@ -61,6 +62,10 @@ public function transformResponseToException(
6162
throw new NotFoundHttpException($this->getResponseMessage($response), $request, $response);
6263
}
6364

65+
if (HttpClient::HTTP_METHOD_NOT_ALLOWED === $response->getStatusCode()) {
66+
throw new MethodNotAllowedHttpException($this->getResponseMessage($response), $request, $response);
67+
}
68+
6469
if (HttpClient::HTTP_UNPROCESSABLE_ENTITY === $response->getStatusCode()) {
6570
throw new UnprocessableEntityHttpException($this->getResponseMessage($response), $request, $response);
6671
}
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 MethodNotAllowedHttpException extends ClientErrorHttpException
10+
{
11+
protected function getAdditionalInformationMessage(): string
12+
{
13+
return '(see https://api.akeneo.com/php-client/exception.html#method-not-allowed-exception)';
14+
}
15+
}

0 commit comments

Comments
 (0)