From c077539937152738798078337143d461491ff93e Mon Sep 17 00:00:00 2001 From: butschster Date: Mon, 8 Jan 2024 14:38:34 +0400 Subject: [PATCH] Add error suppression for cases when invalid data is used for entity retrieval. --- src/Filter/EntityCaster.php | 15 ++++++++++++--- tests/src/Filter/EntityCasterTest.php | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Filter/EntityCaster.php b/src/Filter/EntityCaster.php index a4c9af4..c592dfd 100644 --- a/src/Filter/EntityCaster.php +++ b/src/Filter/EntityCaster.php @@ -6,6 +6,7 @@ use Cycle\ORM\ORMInterface; use Psr\Container\ContainerInterface; +use Spiral\Exceptions\ExceptionReporterInterface; use Spiral\Filters\Exception\SetterException; use Spiral\Filters\Model\FilterInterface; use Spiral\Filters\Model\Mapper\CasterInterface; @@ -20,6 +21,7 @@ final class EntityCaster implements CasterInterface public function __construct( protected readonly ContainerInterface $container, + protected readonly ExceptionReporterInterface $reporter, ) { } @@ -34,11 +36,18 @@ public function supports(\ReflectionNamedType $type): bool public function setValue(FilterInterface $filter, \ReflectionProperty $property, mixed $value): void { - $role = $this->resolveRole($property->getType()); - $object = $this->getOrm()->getRepository($role)->findByPK($value); + try { + $role = $this->resolveRole($property->getType()); + $object = $this->getOrm()->getRepository($role)->findByPK($value); + } catch (\Throwable $e) { + $this->reporter->report($e); + throw new SetterException(previous: $e); + } if ($object === null && !$property->getType()->allowsNull()) { - throw new SetterException(message: \sprintf('Unable to find entity `%s` by primary key "%s"', $role, $value)); + throw new SetterException( + message: \sprintf('Unable to find entity `%s` by primary key "%s"', $role, $value), + ); } $property->setValue($filter, $object); diff --git a/tests/src/Filter/EntityCasterTest.php b/tests/src/Filter/EntityCasterTest.php index 775cdb0..75f00c7 100644 --- a/tests/src/Filter/EntityCasterTest.php +++ b/tests/src/Filter/EntityCasterTest.php @@ -8,6 +8,7 @@ use Spiral\App\Database\Factory\RoleFactory; use Spiral\App\Database\Factory\UserFactory; use Spiral\Cycle\Filter\EntityCaster; +use Spiral\Exceptions\ExceptionReporterInterface; use Spiral\Filters\Exception\SetterException; use Spiral\Tests\DatabaseTest; @@ -24,7 +25,10 @@ public function testExistsRole(): void { $filter = new \ReflectionClass(RoleFilter::class); - $caster = new EntityCaster($this->getContainer()); + $caster = new EntityCaster( + $this->getContainer(), + $this->getContainer()->get(ExceptionReporterInterface::class) + ); $role = RoleFactory::new()->makeOne(); UserFactory::new()->addRole($role)->createOne(); @@ -46,7 +50,10 @@ public function testNonExistRole(): void $filter = new \ReflectionClass(RoleFilter::class); - $caster = new EntityCaster($this->getContainer()); + $caster = new EntityCaster( + $this->getContainer(), + $this->getContainer()->get(ExceptionReporterInterface::class) + ); $property = $filter->getProperty('role'); @@ -59,7 +66,10 @@ public function testNonExistNullableRole(): void { $filter = new \ReflectionClass(RoleFilter::class); - $caster = new EntityCaster($this->getContainer()); + $caster = new EntityCaster( + $this->getContainer(), + $this->getContainer()->get(ExceptionReporterInterface::class) + ); $property = $filter->getProperty('nullableRole');