<?php
namespace Fntv\ApiClientBundle\EventListener;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Fntv\ApiClientBundle\Exception\ApiException;
/**
* Catch les ApiException pour retouner une JsonResponse normalisée
*/
class ApiExceptionListener
{
/**
* @param ExceptionEvent $event
* @return void
*/
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!$exception instanceof ApiException) {
return;
}
$content = [
'error' => $exception->getMessage(),
'details' => $exception->getDetails(),
];
$code = $exception->getCode() ?? Response::HTTP_INTERNAL_SERVER_ERROR;
$event->setResponse(new JsonResponse($content, $code));
}
}