src/Event/ExceptionListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use App\Exception\Core\ConfigurationException;
  4. use Doctrine\DBAL\Exception\ConnectionException;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Twig\Environment;
  11. class ExceptionListener
  12. {
  13.     protected $twig;
  14.     public function __construct(Environment $twig)
  15.     {
  16.         $this->twig $twig;
  17.     }
  18.     public function onKernelException(ExceptionEvent $event)
  19.     {
  20.         $exception $event->getThrowable();
  21.         $context = ['message' => $exception->getMessage()];
  22.         $response = new Response();
  23.         if($exception instanceof AccessDeniedHttpException)
  24.             $response->setContent($this->twig->render('exception/access_denied.html.twig'$context));
  25.         if($exception instanceof ConfigurationException)
  26.             $response->setContent($this->twig->render('exception/configuration.html.twig'$context));
  27.         if($exception instanceof ConnectionException)
  28.             $response->setContent($this->twig->render('exception/db_connection.html.twig'$context));
  29.         if($exception instanceof NotFoundHttpException)
  30.             $response->setContent($this->twig->render('exception/404.html.twig'$context));
  31.         if($response->getContent())
  32.             $event->setResponse($response);
  33.     }
  34. }