<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
class ExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$request = $event->getRequest();
if (in_array('application/json', $request->getAcceptableContentTypes())) {
$response = $this->createApiResponse($exception);
$event->setResponse($response);
}
}
private function createApiResponse(\Exception $exception)
{
$statusCode = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR;
$errors = [];
return new ApiResponse($exception->getMessage(), null, $errors, $statusCode);
}
}