src/Service/Listeners/ExceptionListener.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Service\Listeners;
  3. use Symfony\Component\HttpFoundation\JsonResponse;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  6. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  7. use Symfony\Component\Serializer\SerializerInterface;
  8. class ExceptionListener
  9. {
  10.     private SerializerInterface $serializer;
  11.     public function __construct(SerializerInterface $serializer)
  12.     {
  13.         $this->serializer $serializer;
  14.     }
  15.     public function onKernelException(ExceptionEvent $event): void
  16.     {
  17.         $exception $event->getThrowable();
  18.         // HttpException için özel mesaj belirleme
  19.         if ($exception instanceof HttpExceptionInterface) {
  20.             $message $exception->getStatusCode() === Response::HTTP_REQUEST_ENTITY_TOO_LARGE
  21.                 'The file size is too large. Please upload a smaller file.'
  22.                 $exception->getMessage();
  23.             $statusCode $exception->getStatusCode();
  24.         } else {
  25.             // Diğer tüm beklenmeyen hatalar için
  26.             $message 'An unexpected error occurred.';
  27.             $statusCode Response::HTTP_INTERNAL_SERVER_ERROR;
  28.         }
  29.         // Hata yanıtı JSON olarak hazırlanır
  30.         $data = [
  31.             "message" => $message,
  32.             "severity" => "error",
  33.             "reportable" => $this->getDetailedErrorData($exception)
  34.         ];
  35.         // JSON formatında hata yanıtı oluşturulur
  36.         $json $this->serializer->serialize($data'json', [
  37.             'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS
  38.         ]);
  39.         // Yanıt nesnesi oluşturulur ve etkinleştirilir
  40.         $response = new JsonResponse($json$statusCode, [], true);
  41.         // $event->setResponse($response);
  42.     }
  43.     private function getDetailedErrorData(\Throwable $exception): array
  44.     {
  45.         return [
  46.             "message" => $exception->getMessage(),
  47.             // "trace" => $exception->getTrace(),
  48.             "file" => $exception->getFile(),
  49.             "code" => $exception->getCode(),
  50.             "line" => $exception->getLine(),
  51.             "trace_as_string" => $this->parseErrorReportFromString($exception->getTraceAsString())
  52.         ];
  53.     }
  54.     function parseErrorReportFromString(string $errorString): string {
  55.         // Explode the string by "\n" to process each trace line individually
  56.         $lines explode("\\n"$errorString);
  57.         $parsedReport "Error Trace Report:\n\n";
  58.         foreach ($lines as $lineNumber => $line) {
  59.             // Use regex to capture file path, line number, and function call details
  60.             if (preg_match('/^(.*?)\((\d+)\): (.*)$/'stripslashes($line), $matches)) {
  61.                 $filePath $matches[1];
  62.                 $lineNumber $matches[2];
  63.                 $functionCall $matches[3];
  64.                 // Append parsed information to the report
  65.                 $parsedReport .= "File: $filePath\n";
  66.                 $parsedReport .= "Line: $lineNumber\n";
  67.                 $parsedReport .= "Function Call: $functionCall\n\n";
  68.             } else {
  69.                 $parsedReport .= "Other Info: " stripslashes($line) . "\n";
  70.             }
  71.         }
  72.         return $parsedReport;
  73.     }
  74. }