src/EventSubscriber/API/ExceptionSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\API;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class ExceptionSubscriber implements EventSubscriberInterface
  9. {
  10. /**
  11. * @inheritDoc
  12. */
  13. public static function getSubscribedEvents(): array
  14. {
  15. // return the subscribed events, their methods and priorities
  16. return [
  17. KernelEvents::EXCEPTION => [
  18. ['processException', 10],
  19. ],
  20. ];
  21. }
  22. public function processException(ExceptionEvent $event)
  23. {
  24. if (!$event->getThrowable() instanceof NotFoundHttpException) {
  25. return;
  26. }
  27. $message = preg_replace(
  28. '/(App\\\Entity\\\([^\\\])+\\\)|( by the @ParamConverter annotation)/',
  29. '',
  30. $event->getThrowable()->getMessage()
  31. );
  32. $event->setThrowable(new NotFoundHttpException($message, $event->getThrowable(), Response::HTTP_NOT_FOUND));
  33. }
  34. }