<?php
namespace App\EventSubscriber\API;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
class ExceptionSubscriber implements EventSubscriberInterface
{
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
// return the subscribed events, their methods and priorities
return [
KernelEvents::EXCEPTION => [
['processException', 10],
],
];
}
public function processException(ExceptionEvent $event)
{
if (!$event->getThrowable() instanceof NotFoundHttpException) {
return;
}
$message = preg_replace(
'/(App\\\Entity\\\([^\\\])+\\\)|( by the @ParamConverter annotation)/',
'',
$event->getThrowable()->getMessage()
);
$event->setThrowable(new NotFoundHttpException($message, $event->getThrowable(), Response::HTTP_NOT_FOUND));
}
}