Skip to content

Commit a4029bd

Browse files
committed
Fix stop swallowing exceptions
1 parent 096fefe commit a4029bd

File tree

7 files changed

+44
-74
lines changed

7 files changed

+44
-74
lines changed

Controller/ExceptionController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Dunglas\ApiBundle\Controller;
1313

14+
use Dunglas\ApiBundle\Exception\DeserializationException;
1415
use Dunglas\ApiBundle\JsonLd\Response;
1516
use Symfony\Component\Debug\Exception\FlattenException;
1617
use Symfony\Component\HttpFoundation\Request;
@@ -50,8 +51,8 @@ public function __construct(NormalizerInterface $normalizer)
5051
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
5152
{
5253
if (
53-
'Dunglas\ApiBundle\Exception\DeserializationException' === $exception->getClass()
54-
|| is_subclass_of($exception->getClass(), 'Dunglas\ApiBundle\Exception\DeserializationException')
54+
DeserializationException::class === $exception->getClass() ||
55+
is_subclass_of($exception->getClass(), DeserializationException::class)
5556
) {
5657
$exception->setStatusCode(Response::HTTP_BAD_REQUEST);
5758
}

DependencyInjection/Compiler/TwigExceptionListenerPass.php

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

DunglasApiBundle.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Dunglas\ApiBundle\DependencyInjection\Compiler\DataProviderPass;
1515
use Dunglas\ApiBundle\DependencyInjection\Compiler\ResourcePass;
16-
use Dunglas\ApiBundle\DependencyInjection\Compiler\TwigExceptionListenerPass;
1716
use Symfony\Component\DependencyInjection\ContainerBuilder;
1817
use Symfony\Component\HttpKernel\Bundle\Bundle;
1918

@@ -33,6 +32,5 @@ public function build(ContainerBuilder $container)
3332

3433
$container->addCompilerPass(new ResourcePass());
3534
$container->addCompilerPass(new DataProviderPass());
36-
$container->addCompilerPass(new TwigExceptionListenerPass());
3735
}
3836
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DunglasApiBundle package.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dunglas\ApiBundle\Hydra\EventListener;
13+
14+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
15+
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
16+
17+
/**
18+
* Handle requests errors.
19+
*
20+
* @author Samuel ROZE <[email protected]>
21+
* @author Kévin Dunglas <[email protected]>
22+
*/
23+
class RequestExceptionListener extends ExceptionListener
24+
{
25+
/**
26+
* @param GetResponseForExceptionEvent $event
27+
*/
28+
public function onKernelException(GetResponseForExceptionEvent $event)
29+
{
30+
// Normalize exceptions with hydra errors only for resources
31+
if (!$event->getRequest()->attributes->has('_resource')) {
32+
return;
33+
}
34+
35+
parent::onKernelException($event);
36+
}
37+
}

Resources/config/hydra.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" />
2929
</service>
3030

31-
<service id="api.hydra.listener.exception" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
32-
<argument>api.hydra.controller.exception:showAction</argument>
31+
<service id="api.hydra.listener.request_exception" class="Dunglas\ApiBundle\Hydra\EventListener\RequestExceptionListener">
32+
<argument type="string">api.hydra.controller.exception:showAction</argument>
3333
<argument type="service" id="logger" on-invalid="null" />
3434

35-
<tag name="kernel.event_subscriber" />
35+
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-96" />
3636
<tag name="monolog.logger" channel="request" />
3737
</service>
3838

Tests/DependencyInjection/Compiler/TwigExceptionListenerPassTest.php

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

Tests/DependencyInjection/DunglasApiExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private function getContainerBuilderProphecy($withDoctrine = true)
176176
$containerBuilderProphecy->setDefinition('api.hydra.documentation_builder', $definitionArgument)->shouldBeCalled();
177177
$containerBuilderProphecy->setDefinition('api.hydra.controller.exception', $definitionArgument)->shouldBeCalled();
178178
$containerBuilderProphecy->setDefinition('api.hydra.listener.link_header_response', $definitionArgument)->shouldBeCalled();
179-
$containerBuilderProphecy->setDefinition('api.hydra.listener.exception', $definitionArgument)->shouldBeCalled();
179+
$containerBuilderProphecy->setDefinition('api.hydra.listener.request_exception', $definitionArgument)->shouldBeCalled();
180180
$containerBuilderProphecy->setDefinition('api.hydra.normalizer.collection', $definitionArgument)->shouldBeCalled();
181181
$containerBuilderProphecy->setDefinition('api.hydra.normalizer.constraint_violation_list', $definitionArgument)->shouldBeCalled();
182182
$containerBuilderProphecy->setDefinition('api.hydra.normalizer.error', $definitionArgument)->shouldBeCalled();

0 commit comments

Comments
 (0)