Skip to content

[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

Merged
merged 1 commit into from
Feb 27, 2019

Conversation

gmponos
Copy link
Contributor

@gmponos gmponos commented Jan 10, 2019

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.

@xabbuh
Copy link
Member

xabbuh commented Jan 10, 2019

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
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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)

Copy link
Contributor Author

@gmponos gmponos Jan 10, 2019

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...

Copy link
Contributor

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.

Copy link
Contributor Author

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 :)

@ro0NL
Copy link
Contributor

ro0NL commented Jan 10, 2019

Exceptions should only be thrown by the validators if the developer made some mistake in their configuration

To comply with the API of a constraint validator, implementors should at least know about the Unexpected... exceptions.

@gmponos
Copy link
Contributor Author

gmponos commented Jan 10, 2019

Exceptions should only be thrown by the validators if the developer made some mistake in their configuration and those exceptions should not be caught.

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...

@ro0NL
Copy link
Contributor

ro0NL commented Jan 10, 2019

I suggest something like

@throws UnexpectedTypeException If the constraint is incompatible
@throws UnexpectedTypeException If the value is incompatible (causes an unrecoverable error)
@throws UnexpectedValueException If the value is incompatible (causes a recoverable violation)
@throws ConstraintDefintionException If a constraint option is incompatible during runtime
@throws ValidatorException If any other validation related error occured

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.

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...

In this case, i think you could throw a custom exception as well, and document it on your custom constraint validator.

@gmponos
Copy link
Contributor Author

gmponos commented Jan 10, 2019

@ro0NL I saw that you added above also this

@throws ValidatorException If any other validation related error occured

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...

@gmponos
Copy link
Contributor Author

gmponos commented Jan 14, 2019

I made some changes according to @ro0NL but... I haven't added the UnexpectedValueException. This exception exists on master and I am targeting 3.4

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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 :)

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Jan 24, 2019
dunglas added a commit that referenced this pull request Jan 25, 2019
…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
symfony-splitter pushed a commit to symfony/serializer that referenced this pull request Jan 25, 2019
…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
@gmponos
Copy link
Contributor Author

gmponos commented Feb 26, 2019

Done. I left only one exception as discussed.

@fabpot
Copy link
Member

fabpot commented Feb 27, 2019

Thank you @gmponos.

@fabpot fabpot merged commit 481622d into symfony:3.4 Feb 27, 2019
fabpot added a commit that referenced this pull request Feb 27, 2019
…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
@gmponos gmponos deleted the patch-3 branch February 27, 2019 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants