<?php
namespace App\EventListener;
use App\Controller\Api\ApiController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
class ExceptionListener extends ApiController
{
public function onKernelException(ExceptionEvent $event)
{
// You get the exception object from the received event
$exception = $event->getThrowable();
// Get incoming request
$request = $event->getRequest();
// Check if it is a rest api request
// if ('application/json' === $request->headers->get('Content-Type')) {
// Customize your response object to display the exception details
$response = new JsonResponse([
'line' => $exception->getLine(),
'file' => $exception->getFile(),
'message' => $exception->getMessage(),
'trace_as_html' => $exception->getTraceAsString(),
'code' => $exception->getCode(),
'traces' => $exception->getTrace(),
], Response::HTTP_INTERNAL_SERVER_ERROR);
// $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());
// file_get_contents("https://api.telegram.org/bot5025560542:AAFEB40y5za2hSK9Nw_5EGXwt0QCI388NFo/sendMessage?chat_id=-329774060&parse_mode=Markdown&text=$txt");
// HttpExceptionInterface is a special type of exception that
// holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
// sends the modified response object to the event
$event->setResponse($response);
// }
}
}