vendor/uvdesk/api-bundle/EventListeners/API/KernelException.php line 20

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\ApiBundle\EventListeners\API;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. class KernelException
  9. {
  10.     private $firewall;
  11.     public function __construct(FirewallMap $firewall)
  12.     {
  13.         $this->firewall $firewall;
  14.     }
  15.     public function onKernelException(ExceptionEvent $event)
  16.     {
  17.         $request $event->getRequest();
  18.         $exception $event->getThrowable();
  19.         // Proceed only if we're in the 'uvdesk_api' firewall
  20.         $firewall $this->firewall->getFirewallConfig($request);
  21.         if (empty($firewall) || 'uvdesk_api' != $firewall->getName()) {
  22.             return;
  23.         }
  24.         // Handle api exception accordingly
  25.         switch (true) {
  26.             case $exception instanceof \ErrorException:
  27.                 $responseContent['status'] = false;
  28.                 $responseContent['message'] = 'An unexpected error occurred. Please try again later.';
  29.                 $event->setResponse(new JsonResponse($responseContentResponse::HTTP_INTERNAL_SERVER_ERROR));
  30.                 break;
  31.             case $exception instanceof AccessDeniedHttpException:
  32.                 $responseContent['status'] = false;
  33.                 
  34.                 if (403 === $exception->getStatusCode()) {
  35.                     $responseContent['message'] = 'You\'re not authorized to perform this action.';
  36.                 } else {
  37.                     $responseContent['message'] = $exception->getMessage();
  38.                 }
  39.                 $event->setResponse(new JsonResponse($responseContentResponse::HTTP_FORBIDDEN));
  40.                 break;
  41.             default:
  42.                 $responseContent['status'] = false;
  43.                 $responseContent['message'] = $exception->getMessage();
  44.                 $event->setResponse(new JsonResponse($responseContentResponse::HTTP_INTERNAL_SERVER_ERROR));
  45.                 break;
  46.         }
  47.         
  48.         return;
  49.     }
  50. }