Skip to content

Commit 835db2a

Browse files
committed
Quality enhancement in ErrorController
1 parent f039ca8 commit 835db2a

File tree

4 files changed

+25
-74
lines changed

4 files changed

+25
-74
lines changed

Controller/ExceptionController.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

Controller/HydraController.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111

1212
namespace Dunglas\ApiBundle\Controller;
1313

14+
use Dunglas\ApiBundle\Exception\DeserializationException;
1415
use Dunglas\ApiBundle\JsonLd\Response;
1516
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17+
use Symfony\Component\Debug\Exception\FlattenException;
1618

1719
/**
1820
* Generates a Hydra API documentation.
1921
*
2022
* @author Kévin Dunglas <[email protected]>
23+
* @author Baptiste Meyer <[email protected]>
2124
*/
2225
class HydraController extends Controller
2326
{
@@ -30,4 +33,25 @@ public function docAction()
3033
{
3134
return new Response($this->get('api.hydra.documentation_builder')->getApiDocumentation());
3235
}
36+
37+
/**
38+
* Converts a {@see \Symfony\Component\Debug\Exception\FlattenException}
39+
* to a {@see \Dunglas\ApiBundle\JsonLd\Response}.
40+
*
41+
* @param FlattenException $exception
42+
*
43+
* @return Response
44+
*/
45+
public function exceptionAction(FlattenException $exception)
46+
{
47+
if (is_a($exception->getClass(), DeserializationException::class, true)) {
48+
$exception->setStatusCode(Response::HTTP_BAD_REQUEST);
49+
}
50+
51+
return new Response(
52+
$this->get('serializer')->normalize($exception, 'hydra-error'),
53+
$exception->getStatusCode(),
54+
$exception->getHeaders()
55+
);
56+
}
3357
}

Resources/config/hydra.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
<argument>%api.description%</argument>
1515
</service>
1616

17-
<!-- Controllers -->
18-
19-
<service id="api.hydra.controller.exception" class="Dunglas\ApiBundle\Controller\ExceptionController">
20-
<argument type="service" id="api.hydra.normalizer.error" />
21-
</service>
22-
2317
<!-- Event listeners -->
2418

2519
<service id="api.hydra.listener.link_header_response" class="Dunglas\ApiBundle\Hydra\EventListener\LinkHeaderResponseListener">
@@ -29,7 +23,7 @@
2923
</service>
3024

3125
<service id="api.hydra.listener.request_exception" class="Dunglas\ApiBundle\Hydra\EventListener\RequestExceptionListener">
32-
<argument type="string">api.hydra.controller.exception:showAction</argument>
26+
<argument type="string">DunglasApiBundle:Hydra:exception</argument>
3327
<argument type="service" id="logger" on-invalid="null" />
3428

3529
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-96" />

Tests/DependencyInjection/DunglasApiExtensionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ private function getContainerBuilderProphecy($withDoctrine = true)
174174
$containerBuilderProphecy->setDefinition('api.json_ld.normalizer.datetime', $definitionArgument)->shouldBeCalled();
175175
$containerBuilderProphecy->setDefinition('api.json_ld.encoder', $definitionArgument)->shouldBeCalled();
176176
$containerBuilderProphecy->setDefinition('api.hydra.documentation_builder', $definitionArgument)->shouldBeCalled();
177-
$containerBuilderProphecy->setDefinition('api.hydra.controller.exception', $definitionArgument)->shouldBeCalled();
178177
$containerBuilderProphecy->setDefinition('api.hydra.listener.link_header_response', $definitionArgument)->shouldBeCalled();
179178
$containerBuilderProphecy->setDefinition('api.hydra.listener.request_exception', $definitionArgument)->shouldBeCalled();
180179
$containerBuilderProphecy->setDefinition('api.hydra.normalizer.collection', $definitionArgument)->shouldBeCalled();

0 commit comments

Comments
 (0)