src/Listener/ExceptionListener.php line 33

Open in your IDE?
  1. <?php
  2. /**
  3.  *
  4.  * This file is part of a repository on GitHub.
  5.  *
  6.  * (c) Riccardo De Martis <riccardo@demartis.it>
  7.  *
  8.  * <https://github.com/demartis>
  9.  *
  10.  * For the full copyright and license information, please view the LICENSE
  11.  * file that was distributed with this source code.
  12.  *
  13.  */
  14. namespace App\Listener;
  15. use App\Utils\Jttp\Jttp;
  16. use Psr\Log\LoggerAwareTrait;
  17. use Symfony\Component\Form\FormErrorIterator;
  18. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  19. use Symfony\Component\HttpFoundation\Response;
  20. /**
  21.  * Description of ExceptionListener
  22.  *
  23.  * @author Riccardo De Martis <riccardo@demartis.it>
  24.  */
  25. class ExceptionListener
  26. {
  27.     use LoggerAwareTrait;
  28.     public function onKernelException(ExceptionEvent $event)
  29.     {
  30.         $throwable $event->getThrowable();
  31.         $type get_class($throwable);
  32.         if(method_exists($throwable'getStatusCode')){
  33.             $statusCode $throwable->getStatusCode();
  34.         }else{
  35.             switch ($type){
  36.                 case 'Symfony\Component\Routing\Exception\ResourceNotFoundException':
  37.                     $statusCode Response::HTTP_NOT_FOUND;
  38.                     break;
  39.                 case 'Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException':
  40.                     $statusCode Response::HTTP_NOT_IMPLEMENTED;
  41.                     break;
  42.                 default:
  43.                     $statusCode Response::HTTP_INTERNAL_SERVER_ERROR;
  44.                     break;
  45.             }
  46.         }
  47.         $error=[];
  48.         switch ($type){
  49.             case 'App\Exception\FormException':
  50.                 $data=[];
  51.                 /** @var FormErrorIterator $errors */
  52.                 $errors $throwable->getErrors();
  53.                 foreach ($errors as $e) {
  54.                     $data[$e->getOrigin()->getName()] = $e->getMessage();
  55.                 }
  56.                 $error['form']=$data;
  57.                 break;
  58.             default:
  59.                 $message=$throwable->getMessage();
  60.                 if(gettype($message)=='string' ){
  61.                     $error['detail']=$message;
  62.                 }else{
  63.                     $error=$message;
  64.                 }
  65.                 break;
  66.         }
  67.         $content=Jttp::error($statusCodenull$error)->toArray();
  68.         $response = new Response();
  69.         $response->setStatusCode($statusCode);
  70.         $response->headers->set('Content-Type''application/json');
  71.         $jsonContent json_encode($content);
  72.         if (json_last_error() != JSON_ERROR_NONE) {
  73.             $jsonContent "['error converting to json: ".json_last_error_msg()."']";
  74.         }
  75.         $response->setContent($jsonContent);
  76.         $event->setResponse($response);
  77.     }
  78. //
  79. //    /**
  80. //     * Creates the ApiResponse from any Exception
  81. //     *
  82. //     * @param \Throwable $exception
  83. //     *
  84. //     * @return ApiResponse
  85. //     */
  86. //    private function createApiResponse(\Exception $exception)
  87. //    {
  88. //        $statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR;
  89. //        $errors = [];
  90. //
  91. //        return new ApiResponse($exception->getMessage(), null, $errors, $statusCode);
  92. //    }
  93. }