src/EventSubscriber/ExceptionListener.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Slivki\EventSubscriber;
  4. use Psr\Log\LoggerInterface;
  5. use Slivki\Controller\Admin\BePaid\ValidateBePaidCredentialsAction;
  6. use Slivki\Controller\Api\GiftSubscription\CancelSubscriptionAction;
  7. use Slivki\Controller\Api\OnlineOrder\Vendor\GetNearestTimeAction;
  8. use Slivki\Controller\Api\Profile\Balance\BalanceTransferAction;
  9. use Slivki\Controller\Api\Profile\VirtualWallet\TransferSlivkiPayBalanceAction;
  10. use Slivki\Controller\Payment\Click\CompletePaymentAction;
  11. use Slivki\Controller\Payment\Click\PreparePaymentAction;
  12. use Slivki\Controller\Payment\Oplati\GetStatusOplatiTransactionAction;
  13. use Slivki\Controller\Subscription\GetChildSubscribersAction;
  14. use Slivki\Controller\Subscription\ShareSubscriptionAction;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  18. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  19. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  20. use Throwable;
  21. use function json_decode;
  22. use function array_filter;
  23. use function count;
  24. use function json_last_error;
  25. final class ExceptionListener
  26. {
  27.     private const API_NAMESPACES_START = [
  28.     ];
  29.     private LoggerInterface $logger;
  30.     public function __construct(LoggerInterface $logger)
  31.     {
  32.         $this->logger $logger;
  33.     }
  34.     public function onKernelException(ExceptionEvent $event): void
  35.     {
  36.         $exception $this->getException($event->getThrowable());
  37.         $exceptionMessage $exception->getMessage();
  38.         $controller $event->getRequest()->attributes->get('_controller');
  39.         if (null !== $controller && $this->isIncludedToRoute($controller)) {
  40.             json_decode($exceptionMessagetrue);
  41.             $isJson json_last_error() === JSON_ERROR_NONE;
  42.             $this->logger->error(
  43.                 'Exception occurred in controller',
  44.                 [
  45.                     'exception' => $exception,
  46.                     'isJson' => $isJson,
  47.                     'controller' => $controller,
  48.                 ]
  49.             );
  50.             $response = new JsonResponse(
  51.                 $isJson $exceptionMessage : ['error' => $exceptionMessage],
  52.                 $exception->getCode() !== $exception->getCode() : Response::HTTP_INTERNAL_SERVER_ERROR,
  53.                 [],
  54.                 $isJson
  55.             );
  56.             if ($exception instanceof HttpExceptionInterface) {
  57.                 $response->setStatusCode($exception->getStatusCode());
  58.                 $response->headers->replace($exception->getHeaders());
  59.             }
  60.             $event->setResponse($response);
  61.         }
  62.     }
  63.     private function isIncludedToRoute(string $controller): bool
  64.     {
  65.         $matches array_filter(
  66.             self::API_NAMESPACES_START,
  67.             static fn(string $needle): bool => strpos($controller$needle) !== false,
  68.         );
  69.         return count($matches) > 0;
  70.     }
  71.     private function getException(Throwable $exception): Throwable
  72.     {
  73.         return $exception instanceof HandlerFailedException
  74.             $this->getException($exception->getPrevious())
  75.             : $exception;
  76.     }
  77. }