pimcore/lib/Pimcore/Bundle/AdminBundle/EventListener/AdminExceptionListener.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Enterprise License (PEL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  14.  */
  15. namespace Pimcore\Bundle\AdminBundle\EventListener;
  16. use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
  17. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  18. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  22. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. class AdminExceptionListener implements EventSubscriberInterface
  25. {
  26.     use PimcoreContextAwareTrait;
  27.     /**
  28.      * @inheritDoc
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::EXCEPTION => 'onKernelException'
  34.         ];
  35.     }
  36.     /**
  37.      * Return JSON error responses from webservice context
  38.      *
  39.      * @param GetResponseForExceptionEvent $event
  40.      */
  41.     public function onKernelException(GetResponseForExceptionEvent $event)
  42.     {
  43.         $request $event->getRequest();
  44.         $ex      $event->getException();
  45.         if ($this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_ADMIN)) {
  46.             // only return JSON error for XHR requests
  47.             if (!$request->isXmlHttpRequest()) {
  48.                 return;
  49.             }
  50.             list($code$headers$message) = $this->getResponseData($ex);
  51.             $data = [
  52.                 'success' => false,
  53.                 'message' => $message,
  54.             ];
  55.             if (\Pimcore::inDebugMode()) {
  56.                 $data['trace'] = $ex->getTrace();
  57.             }
  58.             $response = new JsonResponse($data$code$headers);
  59.             $event->setResponse($response);
  60.             return;
  61.         } elseif ($this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_WEBSERVICE)) {
  62.             list($code$headers$message) = $this->getResponseData($ex);
  63.             $data = [
  64.                 'success' => false,
  65.                 'msg'     => $message
  66.             ];
  67.             $response = new JsonResponse($data$code$headers);
  68.             $event->setResponse($response);
  69.             return;
  70.         }
  71.     }
  72.     private function getResponseData(\Exception $exint $defaultStatusCode 500): array
  73.     {
  74.         $code    $defaultStatusCode;
  75.         $headers = [];
  76.         $message $ex->getMessage();
  77.         if ($ex instanceof HttpExceptionInterface) {
  78.             if (empty($message)) {
  79.                 $message Response::$statusTexts[$ex->getStatusCode()];
  80.             }
  81.             $code    $ex->getStatusCode();
  82.             $headers $ex->getHeaders();
  83.         }
  84.         return [$code$headers$message];
  85.     }
  86. }