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

  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\Util\RequestAttributesExtractor;
  13. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  14. use Symfony\Component\HttpKernel\EventListener\ErrorListener;
  15. /**
  16.  * Handles requests errors.
  17.  *
  18.  * @author Samuel ROZE <samuel.roze@gmail.com>
  19.  * @author Kévin Dunglas <dunglas@gmail.com>
  20.  */
  21. final class ExceptionListener
  22. {
  23.     public function __construct(private readonly ErrorListener $errorListener)
  24.     {
  25.     }
  26.     public function onKernelException(ExceptionEvent $event): void
  27.     {
  28.         $request $event->getRequest();
  29.         // Normalize exceptions only for routes managed by API Platform
  30.         if (
  31.             'html' === $request->getRequestFormat('') ||
  32.             !((RequestAttributesExtractor::extractAttributes($request)['respond'] ?? $request->attributes->getBoolean('_api_respond'false)) || $request->attributes->getBoolean('_graphql'false))
  33.         ) {
  34.             return;
  35.         }
  36.         $this->errorListener->onKernelException($event);
  37.     }
  38. }