src/EventListener/AnexoExceptionListener.php line 27

Open in your IDE?
  1. <?php
  2. // src/EventListener/ExceptionListener.php
  3. namespace App\EventListener;
  4. use App\Services\Log\LogInteractionService;
  5. use App\Utils\CommonFunctions;
  6. use App\Utils\Exceptions\CustomException;
  7. use App\Utils\Exceptions\NoPermitidoException;
  8. use Logs\Entity\LogInteraction;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  13. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  14. use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
  15. class AnexoExceptionListener
  16. {
  17.     private $logInteractionService;
  18.     public function __construct(LogInteractionService $logInteractionService)//,LogInteractionService $logInteractionService
  19.     {
  20.         $this->logInteractionService $logInteractionService;
  21.     }
  22.     public function onKernelException(GetResponseForExceptionEvent $event )
  23.     {
  24.         // You get the exception object from the received event
  25.         $exception $event->getException();
  26.         // Customize your response object to display the exception details
  27.         $response = new Response();
  28.         $response->setContent($exception->getMessage());
  29.         // HttpExceptionInterface is a special type of exception that
  30.         // holds status code and header details
  31.         $this->logInteractionService->addLog(
  32.             LogInteraction::$ERROR,
  33.             'ExceptionListener',
  34.             null,
  35.             $exception->getMessage(),
  36.             CommonFunctions::getErrorException($exception)
  37.         );
  38.         switch (true){
  39.             case $exception instanceof AccessDeniedHttpException:
  40.                 $message "No tiene permisos para realizar la operaciĆ³n";
  41.                 $code 403;
  42.                 break;
  43.             case $exception instanceof HttpExceptionInterface:
  44.                 $message $exception->getMessage();
  45.                 $code $exception->getStatusCode();
  46.                 break;
  47.             case $exception instanceof NoPermitidoException:
  48.             case $exception instanceof CustomException:
  49.             case $exception->getCode()==500:
  50.                 $message $exception->getMessage();
  51.                 $code $exception->getCode();
  52.                 break;
  53.             default:
  54.                 $message $exception->getMessage();
  55.                 // $message = "Ha ocurrido un error";
  56.                 $code 500;
  57.         }
  58.         $response = new JsonResponse(['message' => $message],$code);
  59.         // sends the modified response object to the event
  60.         $event->setResponse($response);
  61.     }
  62. }