src/Subscribers/ExceptionEventSubscriber.php line 76

Open in your IDE?
  1. <?php
  2. /**
  3.  *
  4.  * (c) BonBonSlick
  5.  *
  6.  */
  7. declare(strict_types=1);
  8. namespace App\Subscribers;
  9. use App\Exceptions\UserNotFoundException;
  10. use function get_class;
  11. use function in_array;
  12. use Psr\Log\LoggerInterface;
  13. use function sprintf;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\Routing\RouterInterface;
  21. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  22. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  23. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  24. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\HttpFoundation\Request;
  27. /**
  28.  * Class self
  29.  */
  30. final class ExceptionEventSubscriber implements EventSubscriberInterface
  31. {
  32.     /**
  33.      * @var LoggerInterface
  34.      */
  35.     private $logger;
  36.     /**
  37.      * @var RouterInterface
  38.      */
  39.     private $router;
  40.     /**
  41.      * ExceptionListener constructor.
  42.      *
  43.      * @param LoggerInterface $logger
  44.      * @param RouterInterface $router
  45.      */
  46.     public function __construct(LoggerInterface $loggerRouterInterface $router)
  47.     {
  48.         $this->logger $logger;
  49.         $this->router $router;
  50.     }
  51.     /**
  52.      * {@inheritdoc]
  53.      */
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         // return the subscribed events, their methods and priorities
  57.         return [
  58.             KernelEvents::EXCEPTION => [
  59.                 ['processException'10],
  60.             ],
  61.         ];
  62.     }
  63.     /**
  64.      * @param GetResponseForExceptionEvent $event
  65.      *
  66.      * @return void
  67.      */
  68.     public function processException(GetResponseForExceptionEvent $event): void
  69.     {
  70.         // don't do anything if it's not the master request
  71.         if (false === $event->isMasterRequest()) {
  72.             return;
  73.         }
  74.         // return json if json request
  75.         $exception $event->getException();
  76. //        dump($exception);
  77. //        die;
  78.         if (UserNotFoundException::class === get_class($exception)) {
  79.             $response = new JsonResponse(
  80.                 ['error' => true'message' => $exception->getMessage()],
  81.                 Response::HTTP_NOT_FOUND
  82.             );
  83.             $response->headers->set('Content-Type''application/problem+json');
  84.             $event->setResponse($response);
  85.             return;
  86.         }
  87.         if (AccessDeniedException::class === get_class($exception)) {
  88.             $url $this->router->generate('admin.login');
  89.             $response = new RedirectResponse($url);
  90.             $event->setResponse($response);
  91.             return;
  92.         }
  93.         $isExceptionClassInExcludedExceptions in_array(get_class($exception), $this->skipExceptions(), true);
  94.         if (true === $isExceptionClassInExcludedExceptions) {
  95.             $response = new JsonResponse(
  96.                 ['error' => true'message' => $exception->getMessage()],
  97.                 true === array_key_exists($exception->getCode(), Response::$statusTexts) ?
  98.                     $exception->getCode() : Response::HTTP_INTERNAL_SERVER_ERROR
  99.             );
  100.             $response->headers->set('Content-Type''application/problem+json');
  101.             $event->setResponse($response);
  102.         }
  103.         // do nothing if in excluded
  104.         if (true === $isExceptionClassInExcludedExceptions) {
  105.             return;
  106.         }
  107.         // Send the modified response object to the event and log exception
  108.         $logMessage sprintf(
  109.             'EXCEPTION %s. Message: %s. Code: %s',
  110.             $exception->getFile(),
  111.             $exception->getMessage(),
  112.             $exception->getCode()
  113.         );
  114.         $this->logger->error($logMessage);
  115.         $message 'Something went wrong.';
  116.         $url $this->router->generate('homep404');
  117.         $response = new RedirectResponse($url);
  118.         // $response = new JsonResponse(
  119.         //     ['error' => true, 'message' => $message],
  120.         //     Response::HTTP_INTERNAL_SERVER_ERROR
  121.         // );
  122.         // $response->headers->set('Content-Type', 'application/problem+json');
  123.         // $event->setResponse($response);
  124.     }
  125.     /**
  126.      * Avoid logging these exceptions
  127.      *
  128.      * @return string[]
  129.      */
  130.     private function skipExceptions(): array
  131.     {
  132.         return [
  133.             AccessDeniedException::class,
  134.             UnsupportedUserException::class,
  135.             UserNotFoundException::class,
  136.             CustomUserMessageAuthenticationException::class,
  137.             AuthenticationException::class,
  138.         ];
  139.     }
  140. }