Skip to content

[FrameworkBundle] Added friendly exception when constraint validator class does not exist #19601

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

Closed
wants to merge 1 commit into from

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Aug 12, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

Currently when mistakenly we type a Custom Constraint Validator class or the "alias name" from validator service (which would occurs frequently for newcomers) is shown ClassNotFoundException:

Attempted to load class "alias_name" from namespace "Symfony\Component\Validator".
Did you forget a "use" statement for another namespace?
500 Internal Server Error - ClassNotFoundException

This PR tries to improve the error message when this happen.

But I'm not sure about the exception class used (InvalidArgumentException) : ?

The text message more convenient from this little mistake: ?

  • This mistake happen for two reason: FQCN or alias name supplied by constraint not found.
  • The constraint validator service was declared incorrectly (missing alias)
  • Perhaps some hint how the developer should resolve the mistake.

Maybe some documentation core member would help me ?

@yceruto yceruto changed the title Added friendly exception when constraint validator class does not exist [FrameworkBundle] Added friendly exception when constraint validator class does not exist Aug 12, 2016
@yceruto yceruto force-pushed the validator-class-exception branch 2 times, most recently from 234116b to 58a2f49 Compare August 12, 2016 13:24
*/
public function getInstance(Constraint $constraint)
{
$name = $constraint->validatedBy();

if (!isset($this->validators[$name])) {
if (!class_exists($name)) {
throw new InvalidArgumentException(sprintf('Constraint validator class "%s" does not exist.', $name));
Copy link
Member Author

@yceruto yceruto Aug 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the correct exception class in this case? you could improve the text message? see the PR description.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a simple ValidatorException as used in the ValidatorBuilder ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, agree with you.

Now, what do you think about the message ? Should be a little bit friendly ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's friendly enough, but perhaps you can mention the validatedBy method.

@yceruto yceruto force-pushed the validator-class-exception branch 3 times, most recently from d923926 to f070965 Compare August 16, 2016 14:39
@yceruto
Copy link
Member Author

yceruto commented Aug 16, 2016

Updated! both exception class and text message have been improved. @ogizanagi thanks one more time.

@yceruto yceruto force-pushed the validator-class-exception branch 2 times, most recently from d444bed to 80dda7c Compare August 16, 2016 15:02
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
*/
public function getInstance(Constraint $constraint)
{
$name = $constraint->validatedBy();

if (!isset($this->validators[$name])) {
if (!class_exists($name)) {
throw new ValidatorException(sprintf(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on one line please

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed 👍

@yceruto yceruto force-pushed the validator-class-exception branch from 80dda7c to 0142a83 Compare August 23, 2016 12:38
* @throws UnexpectedTypeException When the validator is not an instance of ConstraintValidatorInterface
*/
public function getInstance(Constraint $constraint)
{
$name = $constraint->validatedBy();

if (!isset($this->validators[$name])) {
if (!class_exists($name)) {
throw new ValidatorException(sprintf('Constraint validator "%s" does not exist or it is not enabled. Check the "validatedBy" method in your constraint class "%s"', $name, get_class($constraint)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing dot at the end of the exception message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@fabpot
Copy link
Member

fabpot commented Aug 23, 2016

As it already throws an exception, I think this should be merged in 2.7. ping @symfony/deciders

@yceruto yceruto force-pushed the validator-class-exception branch from 0142a83 to 18cd50c Compare August 23, 2016 19:28
@yceruto
Copy link
Member Author

yceruto commented Aug 23, 2016

@fabpot I should create a separate PR for 2.7 branch?

@HeahDude
Copy link
Contributor

👍 as a bug fix

1 similar comment
@dunglas
Copy link
Member

dunglas commented Aug 23, 2016

👍 as a bug fix

@fabpot
Copy link
Member

fabpot commented Aug 24, 2016

Thank you @yceruto.

fabpot added a commit that referenced this pull request Aug 24, 2016
… validator class does not exist (yceruto)

This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #19601).

Discussion
----------

[FrameworkBundle] Added friendly exception when constraint validator class does not exist

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Currently when mistakenly we type a [Custom Constraint Validator class](http://symfony.com/doc/current/validation/custom_constraint.html#creating-the-validator-itself) or the "alias name" from [validator service](http://symfony.com/doc/current/validation/custom_constraint.html#constraint-validators-with-dependencies)  (which would occurs frequently for newcomers) is shown `ClassNotFoundException`:

> Attempted to load class "alias_name" from namespace "Symfony\Component\Validator".
Did you forget a "use" statement for another namespace?
500 Internal Server Error - ClassNotFoundException

**This PR tries to improve the error message when this happen.**

But I'm not sure about the exception class used ([`InvalidArgumentException`](https://github.com/yceruto/symfony/blob/master/src/Symfony/Component/Validator/Exception/InvalidArgumentException.php)) : ?

*  [`ConstraintDefinitionException`](https://github.com/yceruto/symfony/blob/master/src/Symfony/Component/Validator/Exception/ConstraintDefinitionException.php) would be another option, because the source of the error comes from the custom [`Constraint`](https://github.com/yceruto/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php#L68) definition.

The text message more convenient from this little mistake: ?

* This mistake happen for two reason: FQCN or alias name supplied by constraint not found.
* The constraint validator service was declared incorrectly (missing alias)
* Perhaps some hint how the developer should resolve the mistake.

Maybe some documentation core member would help me ?

Commits
-------

b66ea5e added friendly exception when constraint validator does not exist or it is not enabled
@fabpot
Copy link
Member

fabpot commented Aug 24, 2016

@yceruto FYI, we are able to merge PR on a different branch without you having to create a new PR :)

@fabpot fabpot closed this Aug 24, 2016
@yceruto yceruto deleted the validator-class-exception branch August 24, 2016 12:11
This was referenced Sep 2, 2016
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