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

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\Util\RequestAttributesExtractor;
  13. use Psr\Log\LoggerInterface;
  14. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  15. use Symfony\Component\HttpKernel\EventListener\ErrorListener;
  16. use Symfony\Component\HttpKernel\EventListener\ExceptionListener as LegacyExceptionListener;
  17. /**
  18.  * Handles requests errors.
  19.  *
  20.  * @author Samuel ROZE <samuel.roze@gmail.com>
  21.  * @author Kévin Dunglas <dunglas@gmail.com>
  22.  */
  23. final class ExceptionListener
  24. {
  25.     /**
  26.      * @phpstan-ignore-next-line legacy may not exist
  27.      *
  28.      * @var ErrorListener|LegacyExceptionListener
  29.      */
  30.     private $exceptionListener;
  31.     public function __construct($controllerLoggerInterface $logger null$debug falseErrorListener $errorListener null)
  32.     {
  33.         // @phpstan-ignore-next-line legacy may not exist
  34.         $this->exceptionListener $errorListener ?: new LegacyExceptionListener($controller$logger$debug);
  35.     }
  36.     public function onKernelException(ExceptionEvent $event): void
  37.     {
  38.         $request $event->getRequest();
  39.         // Normalize exceptions only for routes managed by API Platform
  40.         if (
  41.             'html' === $request->getRequestFormat('')
  42.             || !((RequestAttributesExtractor::extractAttributes($request)['respond'] ?? $request->attributes->getBoolean('_api_respond'false)) || $request->attributes->getBoolean('_graphql'false))
  43.         ) {
  44.             return;
  45.         }
  46.         $this->exceptionListener->onKernelException($event); // @phpstan-ignore-line
  47.     }
  48. }
  49. class_alias(ExceptionListener::class, \ApiPlatform\Core\EventListener\ExceptionListener::class);