<?php
namespace App\EventListener;
use Http\Discovery\Exception\NotFoundException;
use Knp\Component\Pager\Exception\PageNumberInvalidException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ErrorListener implements EventSubscriberInterface
{
public function onKernelException(ExceptionEvent $event, string $eventName = null, EventDispatcherInterface $eventDispatcher = null)
{
//if error PageNumberInvalidException (wrong param passed to the paginator), transform it to a 404
$exception = $event->getThrowable();
if($exception instanceof PageNumberInvalidException){
throw new NotFoundException('Not found');
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::EXCEPTION => [
['onKernelException', -128],
],
];
}
}