src/Security/ErrorHandler.php line 39

Open in your IDE?
  1. <?php
  2. // src/Security/ErrorHandler.php
  3. namespace App\Security;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  6. use Twig\Environment;
  7. use Twig\Error\LoaderError;
  8. use Twig\Error\RuntimeError;
  9. use Twig\Error\SyntaxError;
  10. class ErrorHandler
  11. {
  12.     private $twig;
  13.     public function __construct(Environment $twig)
  14.     {
  15.         $this->twig $twig;
  16.     }
  17.     /**
  18.      * @throws RuntimeError
  19.      * @throws SyntaxError
  20.      * @throws LoaderError
  21.      */
  22.     public function handleNotFound(): Response
  23.     {
  24.         $content $this->twig->render('bundles/TwigBundle/Exception/error404.html.twig');
  25.         return new Response($contentResponse::HTTP_NOT_FOUND);
  26.     }
  27.     /**
  28.      * @throws SyntaxError
  29.      * @throws RuntimeError
  30.      * @throws LoaderError
  31.      */
  32.     public function handleError(\Throwable $exception): Response
  33.     {
  34.         $statusCode $exception instanceof HttpExceptionInterface $exception->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR;
  35.         $statusText Response::$statusTexts[$statusCode] ?? 'Unknown error';
  36.         $content $this->twig->render('bundles/TwigBundle/Exception/error.html.twig', [
  37.             'status_code' => $statusCode,
  38.             'status_text' => $statusText,
  39.             'exception' => $exception
  40.         ]);
  41.         return new Response($content$statusCode);
  42.     }
  43. }