src/EventSubscriber/ExceptionListener.php line 44

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.         'Api\V2',
  29.         'UserController::loginAction',
  30.         'Api\Partner',
  31.         'Api\Scheduler',
  32.         'Admin\Api',
  33.         'OnlineOrder\GiftCertificate\EditGiftCertificateOnlineOrderAction',
  34.         'MobileApi\Partner',
  35.         'Api\OnlineOrder\Payment\Method',
  36.         ValidateBePaidCredentialsAction::class,
  37.         GetNearestTimeAction::class,
  38.         ShareSubscriptionAction::class,
  39.         GetChildSubscribersAction::class,
  40.         PreparePaymentAction::class,
  41.         CompletePaymentAction::class,
  42.         GetStatusOplatiTransactionAction::class,
  43.         CancelSubscriptionAction::class,
  44.         BalanceTransferAction::class,
  45.         TransferSlivkiPayBalanceAction::class,
  46.     ];
  47.     private LoggerInterface $logger;
  48.     public function __construct(LoggerInterface $logger)
  49.     {
  50.         $this->logger $logger;
  51.     }
  52.     public function onKernelException(ExceptionEvent $event): void
  53.     {
  54.         $exception $this->getException($event->getThrowable());
  55.         $exceptionMessage $exception->getMessage();
  56.         $controller $event->getRequest()->attributes->get('_controller');
  57.         if (null !== $controller && $this->isIncludedToRoute($controller)) {
  58.             json_decode($exceptionMessagetrue);
  59.             $isJson json_last_error() === JSON_ERROR_NONE;
  60.             $this->logger->error(
  61.                 'Exception occurred in controller',
  62.                 [
  63.                     'exception' => $exception,
  64.                     'isJson' => $isJson,
  65.                     'controller' => $controller,
  66.                 ]
  67.             );
  68.             $response = new JsonResponse(
  69.                 $isJson $exceptionMessage : ['error' => $exceptionMessage],
  70.                 $exception->getCode() !== $exception->getCode() : Response::HTTP_INTERNAL_SERVER_ERROR,
  71.                 [],
  72.                 $isJson
  73.             );
  74.             if ($exception instanceof HttpExceptionInterface) {
  75.                 $response->setStatusCode($exception->getStatusCode());
  76.                 $response->headers->replace($exception->getHeaders());
  77.             }
  78.             $event->setResponse($response);
  79.         }
  80.     }
  81.     private function isIncludedToRoute(string $controller): bool
  82.     {
  83.         $matches array_filter(
  84.             self::API_NAMESPACES_START,
  85.             static fn(string $needle): bool => strpos($controller$needle) !== false,
  86.         );
  87.         return count($matches) > 0;
  88.     }
  89.     private function getException(Throwable $exception): Throwable
  90.     {
  91.         return $exception instanceof HandlerFailedException
  92.             $this->getException($exception->getPrevious())
  93.             : $exception;
  94.     }
  95. }