<?php
namespace App\Listener;
use App\Exception\ApiException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
class ApiExceptionListener
{
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
if (!$exception instanceof ApiException) {
return;
}
$responseData = $exception->getResponse();
$response = new Response();
$response->setContent(json_encode($responseData));
$response->setStatusCode($exception->getCode());
$event->setResponse($response);
}
}