src/EventSubscriber/Api/ExceptionSubscriber.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Api;
  3. use App\Helper\Api\JsonFormatter\Throwable\Formatter;
  4. use App\Helper\Api\Translator\ApiTranslator;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. /**
  10.  * Subscriber for kernel exception to handle most unexpected errors.
  11.  *
  12.  * @package API
  13.  * @internal
  14.  */
  15. class ExceptionSubscriber extends AbstractSubscriber
  16. {
  17.     protected LoggerInterface $logger;
  18.     /**
  19.      * Returns an array of event names this subscriber wants to listen to.
  20.      *
  21.      * The array keys are event names and the value can be:
  22.      *
  23.      *  * The method name to call (priority defaults to 0)
  24.      *  * An array composed of the method name to call and the priority
  25.      *  * An array of arrays composed of the method names to call and respective
  26.      *    priorities, or 0 if unset
  27.      *
  28.      * For instance:
  29.      *
  30.      *  * ['eventName' => 'methodName']
  31.      *  * ['eventName' => ['methodName', $priority]]
  32.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  33.      *
  34.      * The code must not depend on runtime state as it will only be called at compile time.
  35.      * All logic depending on runtime state must be put into the individual methods handling the events.
  36.      *
  37.      * @noinspection PhpArrayShapeAttributeCanBeAddedInspection
  38.      * @noinspection PhpUnused
  39.      */
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         // return the subscribed events, their methods and priorities
  43.         return [
  44.             KernelEvents::EXCEPTION => [
  45.                 ['onKernelException'0]
  46.             ]
  47.         ];
  48.     }
  49.     /**
  50.      * Constructor.
  51.      *
  52.      * @param Formatter $formatter
  53.      * @param ApiTranslator $translator
  54.      * @param LoggerInterface $logger
  55.      */
  56.     public function __construct(Formatter $formatterApiTranslator $translatorLoggerInterface $logger)
  57.     {
  58.         parent::__construct($formatter$translator);
  59.         $this->logger $logger;
  60.     }
  61.     /**
  62.      * Handles most unexpected backend API errors after kernel initialization and ensures a JSON response is given to
  63.      * the frontend.
  64.      *
  65.      * @param ExceptionEvent $event
  66.      * @return void
  67.      * @noinspection PhpUnused
  68.      */
  69.     public function onKernelException(ExceptionEvent $event): void
  70.     {
  71.         // only handle for API routes
  72.         if (preg_match('/^\/api\/|^\/api$/'$event->getRequest()->getRequestUri())) {
  73.             $event->setResponse($this->formatter->format($event->getThrowable()));
  74.             $errorData $this->formatter->getErrorData($event->getThrowable());
  75.             $message $errorData->getHttpStatusCode() . ' ' Response::$statusTexts[$errorData->getHttpStatusCode()];
  76.             if ($errorData->getHttpStatusCode() !== Response::HTTP_UNPROCESSABLE_ENTITY) {
  77.                 $errorData->addData(["request-data" => $event->getRequest()->request->all()]);
  78.             }
  79.             $this->logger->error($message$errorData->jsonSerialize());
  80.         }
  81.     }
  82. }