src/AppBundle/EventListener/ExceptionListener.php line 28

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventListener;
  3. use AppBundle\Exception\CustomValidationException;
  4. use AppBundle\Exception\InvalidInputException;
  5. use AppBundle\Exception\NotAllowedException;
  6. use AppBundle\Exception\ResourceNotFoundException;
  7. use AppBundle\Services\ApiDataCollectorService;
  8. use Pimcore\Log\Simple;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  11. class ExceptionListener
  12. {
  13.     const HTTP_INVALID_PARAMETERS 400;
  14.     const HTTP_NOT_ALLOWED 403;
  15.     const HTTP_RESOURCE_NOT_FOUND 404;
  16.     const HTTP_ERROR_STATUS 500;
  17.     const ROUTE_IDENTIFIER 'api-v1';
  18.     /**
  19.      * @param ExceptionEvent $event
  20.      */
  21.     public function onKernelException(ExceptionEvent $event)
  22.     {
  23.         $pathInfo $event->getRequest()->getPathInfo();
  24.         if (strpos($pathInfo, static::ROUTE_IDENTIFIER) > 0) {
  25.             $exception $event->getThrowable();
  26.             $event->allowCustomResponseCode();
  27.             Simple::log('api-error'$pathInfo ' : ' $exception->getMessage() . PHP_EOL $exception->getTraceAsString());
  28.             $requestContent $event->getRequest()->getContent();
  29.             if(is_string($requestContent)){
  30.                 Simple::log('api-error''request : ' $requestContent );
  31.             }
  32.             if(!empty(ApiDataCollectorService::getData())){
  33.                 Simple::log('api-error''response : ' json_encode(ApiDataCollectorService::getData()) );
  34.             }
  35.             switch (true) {
  36.                 case $exception instanceOf NotAllowedException:
  37.                     $event->setResponse(new JsonResponse([
  38.                         'success' => false,
  39.                         'message' => $exception->getMessage(),
  40.                         'errorFields' => ApiDataCollectorService::getData(),
  41.                         'errorType' => self::HTTP_NOT_ALLOWED,
  42.                     ]));
  43.                     break;
  44.                 case $exception instanceOf ResourceNotFoundException:
  45.                     $event->setResponse(new JsonResponse([
  46.                         'success' => false,
  47.                         'message' => $exception->getMessage(),
  48.                         'errorFields' => ApiDataCollectorService::getData(),
  49.                         'errorType' => self::HTTP_RESOURCE_NOT_FOUND,
  50.                     ]));
  51.                     break;
  52.                 case $exception instanceOf InvalidInputException:
  53.                 case $exception instanceOf CustomValidationException:
  54.                     $event->setResponse(new JsonResponse([
  55.                         'success' => false,
  56.                         'message' => $exception->getMessage(),
  57.                         'errorFields' => ApiDataCollectorService::getData(),
  58.                         'errorType' => self::HTTP_INVALID_PARAMETERS,
  59.                     ]));
  60.                     break;
  61.                 default:
  62.                     $event->setResponse(new JsonResponse([
  63.                         'success' => false,
  64.                         'message' => $exception->getMessage(),
  65.                         'errorFields' => ApiDataCollectorService::getData(),
  66.                         'errorType' => self::HTTP_ERROR_STATUS,
  67.                     ]));
  68.                     break;
  69.             }
  70.         }
  71.     }
  72. }