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($responseContent, Response::HTTP_INTERNAL_SERVER_ERROR));
  30. break;
  31. case $exception instanceof AccessDeniedHttpException:
  32. $responseContent['status'] = false;
  33. if (403 === $exception->getStatusCode()) {
  34. $responseContent['message'] = 'You\'re not authorized to perform this action.';
  35. } else {
  36. $responseContent['message'] = $exception->getMessage();
  37. }
  38. $event->setResponse(new JsonResponse($responseContent, Response::HTTP_FORBIDDEN));
  39. break;
  40. default:
  41. $responseContent['status'] = false;
  42. $responseContent['message'] = $exception->getMessage();
  43. $event->setResponse(new JsonResponse($responseContent, Response::HTTP_INTERNAL_SERVER_ERROR));
  44. break;
  45. }
  46. return;
  47. }
  48. }