Skip to content

Commit

Permalink
Merge pull request #750 from KnpLabs/improve-listener
Browse files Browse the repository at this point in the history
improve exception listener
  • Loading branch information
garak authored Jan 8, 2023
2 parents 92d676e + 50eb7fa commit c016fb7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
"type": "symfony-bundle",
"description": "Paginator bundle for Symfony to automate pagination and simplify sorting and other features",
"keywords": ["pager", "paginator", "pagination", "symfony", "bundle", "knp", "knplabs"],
"homepage": "http://github.com/KnpLabs/KnpPaginatorBundle",
"homepage": "https://github.com/KnpLabs/KnpPaginatorBundle",
"license": "MIT",
"authors": [
{
"name": "KnpLabs Team",
"homepage": "http://knplabs.com"
"homepage": "https://knplabs.com"
},
{
"name": "Symfony Community",
"homepage": "http://github.com/KnpLabs/KnpPaginatorBundle/contributors"
"homepage": "https://github.com/KnpLabs/KnpPaginatorBundle/contributors"
}
],
"require": {
"php": "^8.0",
"knplabs/knp-components": "^4.0",
"knplabs/knp-components": "^4.1",
"symfony/config": "^6.0",
"symfony/dependency-injection": "^6.0",
"symfony/event-dispatcher": "^6.0",
Expand All @@ -28,7 +28,7 @@
"twig/twig": "^3.0"
},
"require-dev": {
"phpstan/phpstan": "^1.8",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5",
"symfony/expression-language": "^6.0",
"symfony/templating": "^6.0"
Expand Down
29 changes: 1 addition & 28 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

namespace Knp\Bundle\PaginatorBundle\EventListener;

use OutOfRangeException;
use Knp\Component\Pager\Exception\InvalidValueException;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use UnexpectedValueException;
use function preg_match;

/**
* Intercept OutOfRangeException/UnexpectedValueException and throw http-related exceptions instead.
Expand All @@ -17,32 +14,8 @@ final class ExceptionListener
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if ($exception instanceof OutOfRangeException) {
if ($exception instanceof \OutOfRangeException || $exception instanceof InvalidValueException) {
$event->setThrowable(new NotFoundHttpException('Not Found.', $exception));
} elseif ($exception instanceof InvalidValueException) {
$event->setThrowable(new NotFoundHttpException('Not Found.', $exception));
} elseif ($exception instanceof UnexpectedValueException && self::isInternalException($exception)) {
$event->setThrowable(new NotFoundHttpException('Not Found', $exception));
}
}

private static function isInternalException(UnexpectedValueException $exception): bool
{
$messages = [
'/^Cannot filter by\: \[.+\] this field is not in allow list$/',
'/^Cannot sort by\: \[.+\] this field is not in allow list\.$/',
'/^Cannot sort with array parameter$/',
'/^ODM query must be a FIND type query$/',
'/^There is no component aliased by \[.+\] in the given Query$/',
'/^There is no component field \[.+\] in the given Query$/',
'/^There is no such field \[.+\] in the given Query component, aliased by \[.+\]$/',
];
foreach ($messages as $regex) {
if (preg_match($regex, $exception->getMessage()) > 0) {
return true;
}
}

return false;
}
}

0 comments on commit c016fb7

Please sign in to comment.