Skip to content

Commit

Permalink
Add error suppression for cases when invalid data is used for entity …
Browse files Browse the repository at this point in the history
…retrieval.
  • Loading branch information
butschster committed Jan 8, 2024
1 parent f0b36a7 commit c077539
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/Filter/EntityCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +21,7 @@ final class EntityCaster implements CasterInterface

public function __construct(
protected readonly ContainerInterface $container,
protected readonly ExceptionReporterInterface $reporter,
) {
}

Expand All @@ -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);

Check warning on line 44 in src/Filter/EntityCaster.php

View check run for this annotation

Codecov / codecov/patch

src/Filter/EntityCaster.php#L42-L44

Added lines #L42 - L44 were not covered by tests
}

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);
Expand Down
16 changes: 13 additions & 3 deletions tests/src/Filter/EntityCasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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');

Expand All @@ -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');

Expand Down

0 comments on commit c077539

Please sign in to comment.