<?php declare(strict_types=1);
namespace App\Pager\Listener;
use Pagerfanta\Exception\NotValidCurrentPageException;
use Pagerfanta\Exception\NotValidMaxPerPageException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Twig\Error\RuntimeError;
class PagerExceptionToHttpExceptionConverter implements EventSubscriberInterface
{
public function onException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!($exception instanceof RuntimeError)) {
return;
}
$previous = $exception->getPrevious();
if (!($previous instanceof NotValidCurrentPageException) && !($previous instanceof NotValidMaxPerPageException)) {
return;
}
$event->setThrowable($previous);
}
/**
* @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
*/
public static function getSubscribedEvents(): array
{
return [
ExceptionEvent::class => ['onException', 1024],
];
}
}