Skip to content

Commit dc77c79

Browse files
committed
fix(symfony): disable symfony error handling by default
1 parent f75649d commit dc77c79

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ private function registerCommonConfiguration(ContainerBuilder $container, array
232232
$container->setParameter('api_platform.collection.pagination.items_per_page_parameter_name', $config['defaults']['pagination_items_per_page_parameter_name'] ?? $config['collection']['pagination']['items_per_page_parameter_name']);
233233
$container->setParameter('api_platform.collection.pagination.partial_parameter_name', $config['defaults']['pagination_partial_parameter_name'] ?? $config['collection']['pagination']['partial_parameter_name']);
234234
$container->setParameter('api_platform.collection.pagination', $this->getPaginationDefaults($config['defaults'] ?? [], $config['collection']['pagination']));
235+
$container->setParameter('api_platform.handle_symfony_errors', $config['handle_symfony_errors'] ?? false);
235236
$container->setParameter('api_platform.http_cache.etag', $config['defaults']['cache_headers']['etag'] ?? true);
236237
$container->setParameter('api_platform.http_cache.max_age', $config['defaults']['cache_headers']['max_age'] ?? null);
237238
$container->setParameter('api_platform.http_cache.shared_max_age', $config['defaults']['cache_headers']['shared_max_age'] ?? null);

src/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public function getConfigTreeBuilder(): TreeBuilder
103103
->booleanNode('force_eager')->defaultTrue()->info('Force join on every relation. If disabled, it will only join relations having the EAGER fetch mode.')->end()
104104
->end()
105105
->end()
106+
->booleanNode('handle_symfony_errors')->defaultFalse()->info('Allows to handle symfony exceptions.')->end()
106107
->booleanNode('enable_swagger')->defaultTrue()->info('Enable the Swagger documentation and export.')->end()
107108
->booleanNode('enable_swagger_ui')->defaultValue(class_exists(TwigBundle::class))->info('Enable Swagger UI')->end()
108109
->booleanNode('enable_re_doc')->defaultValue(class_exists(TwigBundle::class))->info('Enable ReDoc')->end()

src/Symfony/Bundle/Resources/config/symfony/events.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<services>
77
<service id="api_platform.listener.exception" class="ApiPlatform\Symfony\EventListener\ExceptionListener">
88
<argument type="service" id="api_platform.error_listener" on-invalid="null" />
9+
<argument>%api_platform.handle_symfony_errors%</argument>
910

1011
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-96" />
1112
<tag name="monolog.logger" channel="request" />

src/Symfony/EventListener/ExceptionListener.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Symfony\EventListener;
1515

1616
use ApiPlatform\Metadata\Error;
17+
use ApiPlatform\Util\RequestAttributesExtractor;
1718
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1819
use Symfony\Component\HttpKernel\EventListener\ErrorListener;
1920

@@ -25,13 +26,22 @@
2526
*/
2627
final class ExceptionListener
2728
{
28-
public function __construct(private readonly ErrorListener $errorListener)
29+
public function __construct(private readonly ErrorListener $errorListener, public bool $handleSymfonyErrors = false)
2930
{
3031
}
3132

3233
public function onKernelException(ExceptionEvent $event): void
3334
{
3435
$request = $event->getRequest();
36+
37+
// Normalize exceptions only for routes managed by API Platform
38+
if (
39+
false === $this->handleSymfonyErrors &&
40+
!((RequestAttributesExtractor::extractAttributes($request)['respond'] ?? $request->attributes->getBoolean('_api_respond', false)) || $request->attributes->getBoolean('_graphql', false))
41+
) {
42+
return;
43+
}
44+
3545
// Don't loop on errors leave it to Symfony as we could not handle this properly
3646
if (($operation = $request->attributes->get('_api_operation')) && $operation instanceof Error) {
3747
return;

tests/Fixtures/app/config/config_common.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ api_platform:
7373
Symfony\Component\Serializer\Exception\ExceptionInterface: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST
7474
ApiPlatform\Exception\InvalidArgumentException: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST
7575
ApiPlatform\Exception\FilterValidationException: !php/const Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST
76+
handle_symfony_errors: true
7677
http_cache:
7778
invalidation:
7879
enabled: true

0 commit comments

Comments
 (0)