<?php
declare(strict_types = 1);
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Event;
use App\Exception\ApiExceptionInterface;
// LG 20240409 déac use Exception;
// LG 20240409 déac use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use App\PaaBundle\Component\Logs; // LG 20240409
/**
* Description of ExceptionListener
*
* @author avill
*/
class ApiExceptionListener
{
// LG 20240409 déac private $logger;
private $project_dir ; // LG 20240409
// LG 20240409 old public function __construct(LoggerInterface $logger)
public function __construct($project_dir)
{
// LG 20240409 déac $this->logger = $logger;
$this->project_dir = $project_dir; // LG 20240409
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
if (!$event->getException() instanceof ApiExceptionInterface) {
return;
}
$response = new Response($event->getException()->getMessage(), $event->getException()->getStatusCode());
$event->setResponse($response);
$this->log($event->getException());
}
private function log(ApiExceptionInterface $exception){
// LG 20240409 début
//// $log = [
//// 'code' => $exception->getStatusCode(),
//// 'message' => $exception->getMessage(),
//// 'called' => [
//// 'file' => $exception->getTrace()[0]['file'],
//// 'line' => $exception->getTrace()[0]['line'],
//// ],
//// 'occurred' => [
//// 'file' => $exception->getFile(),
//// 'line' => $exception->getLine(),
//// ],
//// ];
////
//// if ($exception->getPrevious() instanceof Exception) {
//// $log += [
//// 'previous' => [
//// 'message' => $exception->getPrevious()->getMessage(),
//// 'exception' => get_class($exception->getPrevious()),
//// 'file' => $exception->getPrevious()->getFile(),
//// 'line' => $exception->getPrevious()->getLine(),
//// ],
//// ];
//// }
// file_put_contents('testArthur2.log', $exception);
//
//
//// var_dump($exception);
//// $this->logger->error(json_encode($log));
$message = sprintf('Erreur APIException : %s avec le code : %s', $exception->getMessage(), $exception->getCode());
Logs::setProjectDir($this->project_dir);
$stackTrace = $exception->getTraceAsString();
$stackTrace = str_replace("#", chr(9) . "#", $stackTrace);
Logs::setStackTrace($stackTrace);
Logs::critical($message);
// LG 20240409 fin
}
}