<?php
namespace App\Api\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class ExceptionSubscriber implements EventSubscriberInterface
{
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
$request = $event->getRequest();
if (0 !== strpos($request->getPathInfo(), '/api')) {
return;
}
if ($exception instanceof HttpException) {
$data = [
'status' => $exception->getStatusCode(),
'message' => $exception->getMessage(),
];
} else {
$data = [
'status' => Response::HTTP_INTERNAL_SERVER_ERROR,
'message' => $exception->getMessage(),
];
}
$event->setResponse(new JsonResponse($data));
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::EXCEPTION => 'onKernelException',
];
}
}