src/EventListener/ErrorListener.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Http\Discovery\Exception\NotFoundException;
  4. use Knp\Component\Pager\Exception\PageNumberInvalidException;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Psr\EventDispatcher\EventDispatcherInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. class ErrorListener implements EventSubscriberInterface
  11. {
  12.     public function onKernelException(ExceptionEvent $eventstring $eventName nullEventDispatcherInterface $eventDispatcher null)
  13.     {
  14.         //if error PageNumberInvalidException (wrong param passed to the paginator), transform it to a 404
  15.         $exception $event->getThrowable();
  16.         if($exception instanceof PageNumberInvalidException){
  17.             throw new NotFoundException('Not found');
  18.         }
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             KernelEvents::EXCEPTION => [
  24.                 ['onKernelException', -128],
  25.             ],
  26.         ];
  27.     }
  28. }