Skip to content

Commit

Permalink
fix: Use FilterValidationException instead of InvalidArgumentExceptio…
Browse files Browse the repository at this point in the history
…n to generate 400 and no 500 errors
  • Loading branch information
ambroisemaupate committed Jul 2, 2023
1 parent 4ee1511 commit ca89b73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/RoadizCoreBundle/src/Api/Filter/ArchiveFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Exception\FilterValidationException;
use Doctrine\ORM\QueryBuilder;

final class ArchiveFilter extends AbstractContextAwareFilter
Expand Down Expand Up @@ -54,10 +54,10 @@ protected function filterProperty(
}

if (!is_string($values[self::PARAMETER_ARCHIVE])) {
throw new InvalidArgumentException(sprintf(
throw new FilterValidationException([sprintf(
'“%s” filter must be only used with a string value.',
self::PARAMETER_ARCHIVE
));
)]);
}

$range = $this->normalizeFilteringDates($values[self::PARAMETER_ARCHIVE]);
Expand Down
7 changes: 3 additions & 4 deletions lib/RoadizCoreBundle/src/Api/Filter/IntersectionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace RZ\Roadiz\CoreBundle\Api\Filter;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Exception\FilterValidationException;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;

Expand All @@ -19,7 +19,6 @@ final class IntersectionFilter extends AbstractContextAwareFilter

/**
* @inheritDoc
* @param mixed $value
*/
protected function filterProperty(
string $property,
Expand All @@ -35,7 +34,7 @@ protected function filterProperty(

foreach ($value as $fieldName => $fieldValue) {
if (empty($fieldName)) {
throw new InvalidArgumentException(sprintf('“%s” filter must be only used with an associative array with fields as keys.', $property));
throw new FilterValidationException([sprintf('“%s” filter must be only used with an associative array with fields as keys.', $property)]);
}
if ($this->isPropertyEnabled($fieldName, $resourceClass)) {
// Allow single value intersection
Expand Down Expand Up @@ -115,7 +114,7 @@ protected function addDuplicatedJoinsForNestedProperty(
}

if (null === $alias) {
throw new InvalidArgumentException(sprintf('Cannot add joins for property "%s" - property is not nested.', $property));
throw new FilterValidationException([sprintf('Cannot add joins for property "%s" - property is not nested.', $property)]);
}

return [$alias, $propertyParts['field'], $propertyParts['associations']];
Expand Down
21 changes: 14 additions & 7 deletions lib/RoadizCoreBundle/src/Api/Filter/LocaleFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace RZ\Roadiz\CoreBundle\Api\Filter;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Exception\FilterValidationException;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -65,13 +65,20 @@ protected function filterProperty(
->getRepository(Translation::class)
->getAvailableLocales();
}
if (count($supportedLocales) === 0) {
throw new FilterValidationException(
['Locale filter is not available because no translation exist.']
);
}
if (!in_array($value, $supportedLocales)) {
throw new InvalidArgumentException(
sprintf(
throw new FilterValidationException(
[sprintf(
'Locale filter value "%s" not supported. Supported values are %s',
$value,
implode(', ', $supportedLocales)
)
)]
);
}
Expand All @@ -93,7 +100,7 @@ protected function filterProperty(
}
if (null === $translation) {
throw new InvalidArgumentException('No translation exist for locale: ' . $value);
throw new FilterValidationException(['No translation exist for locale: ' . $value]);
}

$queryBuilder
Expand Down Expand Up @@ -121,8 +128,8 @@ public function getDescription(string $resourceClass): array
{
$supportedLocales = $this->managerRegistry->getRepository(Translation::class)->getAvailableLocales();
return [
static::PROPERTY => [
'property' => static::PROPERTY,
self::PROPERTY => [
'property' => self::PROPERTY,
'type' => 'string',
'required' => false,
'openapi' => [
Expand Down

0 comments on commit ca89b73

Please sign in to comment.