src/EventSubscriber/ExceptionSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. // src/EventSubscriber/ExceptionSubscriber.php
  3. namespace App\EventSubscriber;
  4. use Twig\Environment;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  10. class ExceptionSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private Environment $twig,
  14.     ) {
  15.     }
  16.     
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         // return the subscribed events, their methods and priorities
  20.         return [
  21.             KernelEvents::EXCEPTION => [
  22.                 ['processException'10],
  23.                 ['logException'0],
  24.                 ['notifyException', -10],
  25.             ],
  26.         ];
  27.     }
  28.     public function processException(ExceptionEvent $event): void
  29.     {        
  30.         $exception $event->getThrowable();
  31.         
  32.         if ($exception->getLine() != 500 && $exception->getCode() != 404) {
  33.             return;
  34.         }
  35.         
  36.         $message sprintf(
  37.             'Error %s. Message: %s',
  38.             $exception->getCode(),
  39.             $exception->getMessage()            
  40.         );
  41.         $response = new Response();
  42.         $response->headers->set('Content-Type''text/html; charset=utf-8');
  43.         $status_code $exception->getCode();
  44.         if ($exception instanceof HttpExceptionInterface) {
  45.             $status_code $exception->getStatusCode();
  46.             if ($status_code == 404) {
  47.                 $message "Page not Found";
  48.             }
  49.             $response->setStatusCode($exception->getStatusCode());
  50.             $response->headers->replace($exception->getHeaders());
  51.         } else {
  52.             $status_code 500;
  53.             $response->setStatusCode(500);
  54.         }
  55.         if ($status_code == 404) {
  56.             $content $this->twig->render('error/error404.html.twig', [
  57.                 'exception' => $exception,
  58.                 'code' => $status_code,
  59.                 'status_code' => $status_code,
  60.                 'message' => $message,            
  61.             ]);
  62.         } else {
  63.             $content $exception->getTraceAsString();
  64.             // $this->twig->render('error/error.html.twig', [
  65.             //     'exception' => $exception,
  66.             //     'code' => $status_code,
  67.             //     'status_code' => $status_code,
  68.             //     'message' => $message,            
  69.             // ]);
  70.         }
  71.         
  72.         $response->setContent($content);        
  73.         $event->setResponse($response);
  74.     }
  75.     public function logException(ExceptionEvent $event): void
  76.     {
  77.         // ...
  78.     }
  79.     public function notifyException(ExceptionEvent $event): void
  80.     {
  81.         // ...
  82.     }
  83. }