src/Noahtech/Sistemas/InterjamaBundle/EventListener/ApiExceptionSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace Noahtech\Sistemas\InterjamaBundle\EventListener;
  3. use Noahtech\Sistemas\InterjamaBundle\Utils\Response as ResponseMC;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  8. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  9. use Symfony\Component\HttpKernel\Exception\HttpException;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  13. use Symfony\Component\HttpFoundation\Response;
  14. /*
  15.  * To change this license header, choose License Headers in Project Properties.
  16.  * To change this template file, choose Tools | Templates
  17.  * and open the template in the editor.
  18.  */
  19. /**
  20.  * Description of ApiExceptionSubscriber
  21.  *
  22.  * @author rafa
  23.  */
  24. class ApiExceptionSubscriber implements EventSubscriberInterface {
  25.     private $templating;
  26.     public function __construct(EngineInterface $templating) {
  27.         $this->templating $templating;
  28.     }
  29.     //put your code here
  30.     public static function getSubscribedEvents(): array {
  31.         return array(
  32.             KernelEvents::EXCEPTION => 'onKernelException'
  33.         );
  34.     }
  35.     public function onKernelException(GetResponseForExceptionEvent $event) {
  36.         $exception $event->getException();
  37. //        $request = $event->getRequest();
  38.         $ajax $event->getRequest()->isXmlHttpRequest();
  39.         $url $event->getRequest()->getPathInfo();
  40.         $api=strpos($url'/api/') !== false// verificamos si viene de api para devolver un rest y no una pagina
  41.         if (!$exception instanceof HttpException) {
  42.             return;
  43.         }
  44.         $code 500;
  45.         if ($exception instanceof HttpException) {
  46.             $code 500;
  47.         }
  48.         if ($exception instanceof NotFoundHttpException) {
  49.             $code 404;
  50.         }
  51.         if ($exception instanceof AccessDeniedHttpException) {
  52.             $code 403;
  53.         }
  54.         if ($exception instanceof AccessDeniedException) {
  55.             $code 403;
  56.         }
  57.         if ($exception instanceof \Symfony\Component\Security\Core\Exception\AccessDeniedException) {
  58.             $code 403;
  59.         }
  60.         if ($ajax || $api) {
  61.             $response = new ResponseMC();
  62.             $response->setCode($code);
  63.             $response->setMessage($exception->getMessage());
  64.             $res = new JsonResponse(['code' => $response->getCode(),'error'=>1,
  65.                 'message' => $response->getMessage()
  66.             ]);
  67.             $res->headers->set('Content-Type''application/problem+json');
  68.         } else {
  69.             $title "No se encuentra autorizado para acceder a la pagina.";
  70.             $res = new Response($this->templating->render('@NoahtechSistemasInterjama/Default/error.html.twig',
  71.                             array('title' => $title'error' => 1), 200));
  72.         }
  73.         $event->setResponse($res);
  74.     }
  75. }