<?php
namespace App\Subscriber;
use App\Exception\NormalizedExceptionInterface;
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\KernelEvents;
/**
* Class ExceptionSubscriber.
*/
class ExceptionSubscriber implements EventSubscriberInterface
{
const SERVER_ERROR = 'server_error';
const FORBIDDEN = 'forbidden';
public function processException(ExceptionEvent $event)
{
/** @var \Exception $exception */
$exception = $event->getThrowable();
if (!($exception instanceof NormalizedExceptionInterface)) {
return;
}
$result = $exception->normalize();
$httpCode = $exception->getHttpCode();
$event->setResponse(new JsonResponse($result, $httpCode));
}
/**
* @return string[][][]|number[][][]
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::EXCEPTION => [['processException', 255]],
];
}
}