<?php
/* @author <storageprocedure@gmail.com> */
declare(strict_types=1);
namespace App\EventSubscriber;
use App\Exception\ApplicationLogicException;
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 implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::EXCEPTION => [
['processApplicationLogicException', 10],
// ['logException', 0],
// ['notifyException', -10],
],
];
}
public function processApplicationLogicException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
if (!$exception instanceof ApplicationLogicException) {
return;
}
$value = $exception->getMessages();
$value = implode('<br/> ', $value);
$response = new JsonResponse($value, Response::HTTP_BAD_REQUEST);
$response->setEncodingOptions(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$event->setResponse($response);
}
}