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

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\Core\EventListener;
  12. use ApiPlatform\Core\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.      * @var ErrorListener
  27.      */
  28.     private $exceptionListener;
  29.     public function __construct($controllerLoggerInterface $logger null$debug falseErrorListener $errorListener null)
  30.     {
  31.         $this->exceptionListener $errorListener ? new ErrorListener($controller$logger$debug) : new LegacyExceptionListener($controller$logger$debug); // @phpstan-ignore-line
  32.     }
  33.     public function onKernelException(ExceptionEvent $event): void
  34.     {
  35.         $request $event->getRequest();
  36.         // Normalize exceptions only for routes managed by API Platform
  37.         if (
  38.             'html' === $request->getRequestFormat('') ||
  39.             !((RequestAttributesExtractor::extractAttributes($request)['respond'] ?? $request->attributes->getBoolean('_api_respond'false)) || $request->attributes->getBoolean('_graphql'false))
  40.         ) {
  41.             return;
  42.         }
  43.         $this->exceptionListener->onKernelException($event);
  44.     }
  45. }