<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
class ExceptionSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
// return the subscribed events, their methods and priorities
return [
KernelEvents::EXCEPTION =>'onKernelExecption',
];
}
public function onKernelExecption(ExceptionEvent $event)
{
$exception = $event->getThrowable();
$response = new JsonResponse([
'code' => Response::HTTP_INTERNAL_SERVER_ERROR,
'message' => $exception->getMessage()
]);
$event->setResponse($response);
}
}