Skip to content

Commit 018356b

Browse files
author
Vladyslav Yarysh
committed
update errors handling
1 parent 29b490a commit 018356b

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

‎Validator/Constraints/Entity/EntityExistsValidator.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace StfalconStudio\ApiBundle\Validator\Constraints\Entity;
1414

15+
use StfalconStudio\ApiBundle\Exception\LogicException;
1516
use StfalconStudio\ApiBundle\Exception\Validator\UnexpectedConstraintException;
1617
use StfalconStudio\ApiBundle\Traits\EntityManagerTrait;
1718
use Symfony\Component\Validator\Constraint;
@@ -36,13 +37,21 @@ public function validate(mixed $value, Constraint|EntityExists $constraint): voi
3637
throw new UnexpectedConstraintException($constraint, EntityExists::class);
3738
}
3839

39-
if (!(\is_int($value) || \is_string($value))) {
40-
return;
40+
$entityClass = $constraint->class;
41+
42+
try {
43+
$this->em->getClassMetadata($entityClass);
44+
} catch (\Exception $exception) {
45+
throw new LogicException(sprintf('Class %s is not an Entity', $entityClass));
4146
}
4247

4348
$repository = $this->em->getRepository($constraint->class);
4449

4550
if (!$repository->findOneBy([$constraint->property => $value]) instanceof $constraint->class) {
51+
if (!(\is_int($value) || \is_string($value) || $value instanceof \Stringable)) {
52+
throw new LogicException(sprintf('Value expected to be int, string or implement %s to find cause of the problem', \Stringable::class));
53+
}
54+
4655
$this->context
4756
->buildViolation($constraint->message)
4857
->setCode(EntityExists::ENTITY_DOES_NOT_EXIST)

0 commit comments

Comments
 (0)