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\Payment\Click\CompletePaymentAction;
  9. use Slivki\Controller\Payment\Click\PreparePaymentAction;
  10. use Slivki\Controller\Payment\Oplati\GetStatusOplatiTransactionAction;
  11. use Slivki\Controller\Subscription\GetChildSubscribersAction;
  12. use Slivki\Controller\Subscription\ShareSubscriptionAction;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  16. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  17. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  18. use function json_decode;
  19. final class ExceptionListener
  20. {
  21.     private const API_NAMESPACES_START = [
  22.         'Api\V2',
  23.         'UserController::loginAction',
  24.         'Api\Partner',
  25.         'Api\Scheduler',
  26.         'Admin\Api',
  27.         'OnlineOrder\GiftCertificate\EditGiftCertificateOnlineOrderAction',
  28.         'MobileApi\Partner',
  29.         'Api\OnlineOrder\Payment\Method',
  30.         ValidateBePaidCredentialsAction::class,
  31.         GetNearestTimeAction::class,
  32.         ShareSubscriptionAction::class,
  33.         GetChildSubscribersAction::class,
  34.         PreparePaymentAction::class,
  35.         CompletePaymentAction::class,
  36.         GetStatusOplatiTransactionAction::class,
  37.         CancelSubscriptionAction::class,
  38.     ];
  39.     private LoggerInterface $logger;
  40.     public function __construct(LoggerInterface $logger)
  41.     {
  42.         $this->logger $logger;
  43.     }
  44.     public function onKernelException(ExceptionEvent $event): void
  45.     {
  46.         $exception $this->getException($event->getThrowable());
  47.         $exceptionMessage $exception->getMessage();
  48.         $controller $event->getRequest()->attributes->get('_controller');
  49.         if (null !== $controller && $this->isIncludedToRoute($controller)) {
  50.             json_decode($exceptionMessagetrue);
  51.             $isJson = \json_last_error() === JSON_ERROR_NONE;
  52.             $this->logger->error(
  53.                 'Exception occurred in controller',
  54.                 [
  55.                     'exception' => $exception,
  56.                     'isJson' => $isJson,
  57.                     'controller' => $controller,
  58.                 ]
  59.             );
  60.             $response = new JsonResponse(
  61.                 $isJson $exceptionMessage : ['error' => $exceptionMessage],
  62.                 $exception->getCode() !== $exception->getCode() : Response::HTTP_INTERNAL_SERVER_ERROR,
  63.                 [],
  64.                 $isJson
  65.             );
  66.             if ($exception instanceof HttpExceptionInterface) {
  67.                 $response->setStatusCode($exception->getStatusCode());
  68.                 $response->headers->replace($exception->getHeaders());
  69.             }
  70.             $event->setResponse($response);
  71.         }
  72.     }
  73.     private function isIncludedToRoute(string $controller): bool
  74.     {
  75.         $matches = \array_filter(
  76.             self::API_NAMESPACES_START,
  77.             static fn(string $needle): bool => strpos($controller$needle) !== false,
  78.         );
  79.         return \count($matches) > 0;
  80.     }
  81.     private function getException(\Throwable $exception): \Throwable
  82.     {
  83.         return $exception instanceof HandlerFailedException
  84.             $this->getException($exception->getPrevious())
  85.             : $exception;
  86.     }
  87. }