-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Docblock about throwing a validator Exception #29832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Exceptions should only be thrown by the validators if the developer made some mistake in their configuration and those exceptions should not be caught. Thus I would not document them here. |
@@ -30,6 +31,8 @@ public function initialize(ExecutionContextInterface $context); | |||
* | |||
* @param mixed $value The value that should be validated | |||
* @param Constraint $constraint The constraint for the validation | |||
* | |||
* @throws ValidatorException |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yet some implementations are tied to the constraint or metadata factory even..
- NoSuchMetadataException
- MissingOptionsException
I'd prefer the expected ones here:
- UnexpectedTypeException
- UnexpectedValueException
- ConstraintDefinitionException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens in cases that I have a custom validator that throws a custom exception extending the ValidatorException
.
Also I don't get what is the issue with NoSuchMetadataException
or MissingOptionsException
since they both extend ValidatorException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constraint validators never throw such an exception, at least not in core.
We cant just add ANY exception the user might throw, only the expected ones (which the high level validator deals with)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cant just add ANY exception
What I have added here is not ANY exception.. It is a specific exception that ANY exception implemented by a developer it must extend it...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok put different :) i think ConstraintValidatorInterface::validate()
is designed to throw specific exceptions, for specific cases. Not just any ValidatorException
.. esp. since the high level validator checks specific ones, e.g. UnexpectedValueException
Throwing ValidatorException instead of UnexpectedValueException would be a wrong implementation. As such i think we should document the specific cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the argument is understandable.. although I disagree :)
To comply with the API of a constraint validator, implementors should at least know about the |
Why not.. ?? I might have a controller that initializes my custom validator and I need to customize the message shown to the consumer of the controller by catching the exception... |
I suggest something like
im :+-0: on adding ValidatorException specifically; i've no real proof it was designed like that. It seems reasonable, though in practice it could be a plain \RuntimeException as well. Which is why im skeptical about adding it now.
In this case, i think you could throw a custom exception as well, and document it on your custom constraint validator. |
@ro0NL I saw that you added above also this
I can agree with that then.. I thought that in your review comment you you wanted to remove the generic one and add only the specific ones... |
I made some changes according to @ro0NL but... I haven't added the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not extra convinced, but no objections either :)
…lizer (gmponos) This PR was squashed before being merged into the 3.4 branch (closes #29889). Discussion ---------- [Serializer] Docblock about throwing exceptions on serializer | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Most of the serializers can throw a `\Symfony\Component\Serializer\ExceptionInterface`. This makes the docblock in line with that. Similar to this #29832 Commits ------- 0b44ad7 [Serializer] Docblock about throwing exceptions on serializer
…lizer (gmponos) This PR was squashed before being merged into the 3.4 branch (closes #29889). Discussion ---------- [Serializer] Docblock about throwing exceptions on serializer | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Most of the serializers can throw a `\Symfony\Component\Serializer\ExceptionInterface`. This makes the docblock in line with that. Similar to this symfony/symfony#29832 Commits ------- 0b44ad79c6 [Serializer] Docblock about throwing exceptions on serializer
src/Symfony/Component/Validator/ConstraintValidatorInterface.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/ConstraintValidatorInterface.php
Outdated
Show resolved
Hide resolved
Done. I left only one exception as discussed. |
Thank you @gmponos. |
…n (gmponos) This PR was squashed before being merged into the 3.4 branch (closes #29832). Discussion ---------- [Validator] Docblock about throwing a validator Exception | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Most of the validators if I remember correctly can throw a `ValidatorException`. This makes the docblock in line with that. Commits ------- 481622d [Validator] Docblock about throwing a validator Exception
Most of the validators if I remember correctly can throw a
ValidatorException
. This makes the docblock in line with that.