vendor/easycorp/easyadmin-bundle/src/EventListener/ExceptionListener.php line 35

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\EventListener;
  3. use EasyCorp\Bundle\EasyAdminBundle\Exception\BaseException;
  4. use EasyCorp\Bundle\EasyAdminBundle\Exception\FlattenException;
  5. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Twig\Environment;
  9. use Twig\Error\RuntimeError;
  10. /**
  11.  * This listener allows to display customized error pages in the production
  12.  * environment.
  13.  *
  14.  * @internal
  15.  *
  16.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  17.  * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  18.  */
  19. final class ExceptionListener
  20. {
  21.     private $kernelDebug;
  22.     private $adminContextProvider;
  23.     private $twig;
  24.     public function __construct(bool $kernelDebugAdminContextProvider $adminContextProviderEnvironment $twig)
  25.     {
  26.         $this->kernelDebug $kernelDebug;
  27.         $this->adminContextProvider $adminContextProvider;
  28.         $this->twig $twig;
  29.     }
  30.     public function onKernelException(ExceptionEvent $event)
  31.     {
  32.         $exception $event->getThrowable();
  33.         if ($this->kernelDebug && $exception instanceof RuntimeError && 'Variable "ea" does not exist.' === $exception->getRawMessage()) {
  34.             $exception->appendMessage($this->getEaVariableExceptionMessage());
  35.             return;
  36.         }
  37.         if ($this->kernelDebug || !$exception instanceof BaseException) {
  38.             return;
  39.         }
  40.         // TODO: check why these custom error pages don't work
  41.         $event->setResponse($this->createExceptionResponse(FlattenException::create($exception)));
  42.     }
  43.     public function createExceptionResponse(FlattenException $exception): Response
  44.     {
  45.         $context $this->adminContextProvider->getContext();
  46.         $exceptionTemplatePath null === $context '@EasyAdmin/exception.html.twig' $context->getTemplatePath('exception');
  47.         $layoutTemplatePath null === $context '@EasyAdmin/layout.html.twig' $context->getTemplatePath('layout');
  48.         return new Response($this->twig->render($exceptionTemplatePath, [
  49.             'exception' => $exception,
  50.             'layout_template_path' => $layoutTemplatePath,
  51.         ]), $exception->getStatusCode());
  52.     }
  53.     private function getEaVariableExceptionMessage(): string
  54.     {
  55.         return <<<MESSAGE
  56. The "ea" variable stores the admin context (menu items, actions, fields, etc.) and it's created automatically for requests served by EasyAdmin.
  57. If you are seeing this error, you are trying to use some EasyAdmin features in a request not served by EasyAdmin. For example, some of your custom actions may be trying to render or extend from one of the templates provided EasyAdmin.
  58. Your request must meet one of these conditions to be served by EasyAdmin (and to have the "ea" variable defined):
  59. 1) It must be run by a controller that implements DashboardControllerInterface. This is done automatically for all actions and CRUD controllers associated to your dashboard.
  60. 2) It must contain an "eaContext" query string parameter that identifies the Dashboard associated to this request (this parameter is automatically added by EasyAdmin when creating menu items that link to custom Symfony routes).
  61. MESSAGE;
  62.     }
  63. }