src/EventListener/ExceptionListener.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Controller\Api\ApiController;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  8. class ExceptionListener extends ApiController
  9. {
  10.     public function onKernelException(ExceptionEvent $event)
  11.     {
  12.         
  13.         // You get the exception object from the received event
  14.         $exception $event->getThrowable();
  15.         // Get incoming request
  16.         $request   $event->getRequest();
  17.         
  18.         // Check if it is a rest api request
  19.         // if ('application/json' === $request->headers->get('Content-Type')) {
  20.             // Customize your response object to display the exception details
  21.             $response = new JsonResponse([
  22.                 'line'          => $exception->getLine(),
  23.                 'file'          => $exception->getFile(),
  24.                 'message'       => $exception->getMessage(),
  25.                 'trace_as_html' => $exception->getTraceAsString(),
  26.                 'code'          => $exception->getCode(),
  27.                 'traces'        => $exception->getTrace(),
  28.             ], Response::HTTP_INTERNAL_SERVER_ERROR);
  29.             // $txt = urlencode("*error* \n \n *line:* ".$exception->getLine(). " \n \n *User exeption:* \n".$this->getUser(). " \n \n *message:*".$exception->getMessage()." \n \n *file:* ".$exception->getFile()." \n \n *trace:* ".$exception->getTraceAsString());
  30.             // file_get_contents("https://api.telegram.org/bot5025560542:AAFEB40y5za2hSK9Nw_5EGXwt0QCI388NFo/sendMessage?chat_id=-329774060&parse_mode=Markdown&text=$txt");
  31.             // HttpExceptionInterface is a special type of exception that
  32.             // holds status code and header details
  33.             if ($exception instanceof HttpExceptionInterface) {
  34.                 $response->setStatusCode($exception->getStatusCode());
  35.                 $response->headers->replace($exception->getHeaders());
  36.             } else {
  37.                 $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
  38.             }
  39.             // sends the modified response object to the event
  40.             $event->setResponse($response);
  41.         // }
  42.     }
  43. }