src/EventListener/ApiExceptionListener.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Exception\ApiExceptionInterface;
  4. use Doctrine\ODM\MongoDB\DocumentManager;
  5. use Exception;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Throwable;
  11. use Twig\Environment;
  12. class ApiExceptionListener
  13. {
  14.     /**
  15.      * @var TranslatorInterface
  16.      */
  17.     private $translator;
  18.     /**
  19.      * @var Environment
  20.      */
  21.     private $twig;
  22.     /**
  23.      * ApiExceptionListener constructor.
  24.      * @param Environment $twig
  25.      * @param DocumentManager $documentManager
  26.      * @param TranslatorInterface $translator
  27.      */
  28.     public function __construct(Environment $twig,
  29.                                 DocumentManager $documentManager,
  30.                                 TranslatorInterface $translator
  31.     )
  32.     {
  33.         $this->translator $translator;
  34.         $this->twig $twig;
  35.     }
  36.     /**
  37.      * @param ExceptionEvent $event
  38.      * @return bool
  39.      * @throws Exception
  40.      */
  41.     public function onKernelException(ExceptionEvent $event)
  42.     {
  43.         $statusCode 500;
  44.         if ($event->getThrowable() instanceof ApiExceptionInterface) {
  45.             $statusCode $event->getThrowable()->getCode();
  46.         }
  47.         if (!$event->getRequest()->attributes->get("_controller")) {
  48.             return false;
  49.         }
  50.         $system explode('\\'$event->getRequest()->attributes->get("_controller"))[2];
  51.         $contentType $event->getRequest()->headers->get('Content-Type');
  52.         if ($system == 'Api') {
  53.             $response $this->buildResponseData($event->getThrowable());
  54.             $jsonResponse = new JsonResponse($response);
  55.             $jsonResponse->setStatusCode($statusCode);
  56.             $event->setResponse($jsonResponse);
  57.         } else {
  58.             $viewResponse $this->twig->render('error/' $statusCode '.html.twig'$this->buildResponseData($event->getThrowable()));
  59.             $event->setResponse(new Response($viewResponse));
  60.         }
  61.         return true;
  62.     }
  63.     /**
  64.      * @param Throwable $exception
  65.      * @return array
  66.      */
  67.     private function buildResponseData(Throwable $exception)
  68.     {
  69.         $response $this::normalizeException($exception);
  70.         if (get_class($exception) !== 'Booklogic\CoreBundle\Exception\ValidationException') {
  71.             $response['error']['message'] = $this->translator->trans($response['error']['message'], [], 'exceptions');
  72.         }
  73.         return $response;
  74.     }
  75.     public static function normalizeException(Throwable $exception)
  76.     {
  77.         $customMessage json_decode($exception->getMessage(), 1);
  78.         if (is_array($customMessage)) {
  79.             $message $customMessage['message'];
  80.             $code $customMessage['code'];
  81.             if (isset($customMessage['audit'])) {
  82.                 $audit $customMessage['audit'];
  83.             }
  84.         } else {
  85.             $message $exception->getMessage();
  86.             $code $exception->getCode();
  87.         }
  88.         $response = [
  89.             'error' => [
  90.                 'code' => (string)$code,
  91.                 'message' => $message,
  92.             ]
  93.         ];
  94.         if ($_ENV['APP_ENV'] == 'dev') {
  95.             $response['error']['detail'] = [
  96.                 'file' => $exception->getFile(),
  97.                 'line' => $exception->getLine(),
  98.             ];
  99.         }
  100.         if (isset($audit)) {
  101.             $response['audit'] = $audit;
  102.         }
  103.         return $response;
  104.     }
  105. }