<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use App\Services\ErrorHandling\ApiErrorResponseHandler;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ExceptionListener implements EventSubscriberInterface
{
/**
* @var ApiErrorResponseHandler
*/
private $errorResponseHandler;
public function __construct(ApiErrorResponseHandler $errorResponseHandler)
{
$this->errorResponseHandler = $errorResponseHandler;
}
public function onKernelException(ExceptionEvent $event): void
{
$event->setResponse($this->errorResponseHandler->handle($event->getThrowable()));
$event->allowCustomResponseCode();
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::EXCEPTION => ['onKernelException', -100],
];
}
}