vendor/api-platform/core/src/Symfony/EventListener/ExceptionListener.php line 33

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\EventListener;
  12. use ApiPlatform\Metadata\Error;
  13. use ApiPlatform\Util\RequestAttributesExtractor;
  14. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  15. use Symfony\Component\HttpKernel\EventListener\ErrorListener;
  16. /**
  17.  * Handles requests errors.
  18.  *
  19.  * @author Samuel ROZE <samuel.roze@gmail.com>
  20.  * @author Kévin Dunglas <dunglas@gmail.com>
  21.  */
  22. final class ExceptionListener
  23. {
  24.     public function __construct(private readonly ErrorListener $errorListener, public bool $handleSymfonyErrors false)
  25.     {
  26.     }
  27.     public function onKernelException(ExceptionEvent $event): void
  28.     {
  29.         $request $event->getRequest();
  30.         // Normalize exceptions only for routes managed by API Platform
  31.         if (
  32.             false === $this->handleSymfonyErrors
  33.             && !((RequestAttributesExtractor::extractAttributes($request)['respond'] ?? $request->attributes->getBoolean('_api_respond'false)) || $request->attributes->getBoolean('_graphql'false))
  34.         ) {
  35.             return;
  36.         }
  37.         // Don't loop on errors leave it to Symfony as we could not handle this properly
  38.         if (($operation $request->attributes->get('_api_operation')) && $operation instanceof Error) {
  39.             return;
  40.         }
  41.         $this->errorListener->onKernelException($event);
  42.     }
  43. }