Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DX: better error message if factory class is empty #18547

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
Apr 18, 2016
Merged

DX: better error message if factory class is empty #18547

merged 1 commit into from
Apr 18, 2016

Conversation

dbu
Copy link
Contributor

@dbu dbu commented Apr 14, 2016

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

i was trying to debug this service configuration:

        <service id="repository.user" class="Doctrine\ORM\EntityRepository">
            <argument>%user_entity_class%</argument>
            <factory id="doctrine.orm.default_entity_manager" method="getRepository"/>
        </service>

Turns out i should have used <factory service=... instead of <factory id=... but the error message does not even tell which service is wrong. The same happens when only specifying the method= attribute. The current exception message tells:

Cannot dump definition because of invalid class name ('')

With this change, the message at least tells the context, hopefully helping the developer debug the issue:

Empty class for factory of service repository.user and no factory service specified

@dbu dbu changed the title better error message if factory class is empty DX: better error message if factory class is empty Apr 14, 2016
@javiereguiluz javiereguiluz added DependencyInjection Feature DX DX = Developer eXperience (anything that improves the experience of using Symfony) labels Apr 14, 2016
@nicolas-grekas
Copy link
Member

@iltar (and @symfony/deciders ) would be better to vote using real comments IMHO, so the other deciders can get a notification about it, WDYT?
👍 even for 3.1 on my side also

@stof
Copy link
Member

stof commented Apr 14, 2016

@nicolas-grekas agreed about deciders (otherwise GH won't take votes into account either btw). This is even required for veto votes btw, as we must explain them (and we cannot with reactions), and it also allows to see the history of votes of the core team (for vote changes for instance). However, @iltar is not a decider, so it is fine to show support through reactions IMO (avoids spamming the discussion and shows a total of votes)

👍 for this

@nicolas-grekas
Copy link
Member

Oups, I thought @iltar you were a decider sorry :)

@linaori
Copy link
Contributor

linaori commented Apr 14, 2016

@stof explains pretty much why I use that button instead ^.^

I was a bit in a hurry to write a proper reply. I think the sentence is a bit hard to read, what about something like:

The "repository.user" service is defined as factory but is missing the service 
reference, did you forget to define the factory service id?

@stof
Copy link
Member

stof commented Apr 14, 2016

@nicolas-grekas I think we should even validate this earlier, in the XML and Yaml loader, as they can give more detailed feedback. What do you think about that ?
This does not prevent having a check here for the case where the service definition is configured elsewhere

@xabbuh
Copy link
Member

xabbuh commented Apr 14, 2016

Actually, I was wondering why the example XML did pass the XSD check at all. Turns out the callable type allows to define an id attribute. Does anyone know why this was added initially?

@dbu
Copy link
Contributor Author

dbu commented Apr 14, 2016 via email

@nicolas-grekas
Copy link
Member

I agree this comes late in the process. Until someone fixes that, let's enhance the message here, I like @iltar suggestion.

@@ -692,7 +692,7 @@ private function addServices()
return $publicServices.$privateServices;
}

private function addNewInstance(Definition $definition, $return, $instantiation)
private function addNewInstance(Definition $definition, $return, $instantiation, $id)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i realized that the id parameter was removed since symfony 2 (i tested this in my project with symfony 2...)

not sure if its ok to pass the id around just for the purpose of the error message, but without the id the message becomes a lot less useful

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a private method so I don't think it really matters that much. For the sake of DX, I'd say it's a good case to pass it along because it's required to build the error message.

@nicolas-grekas
Copy link
Member

@dbu tests need some love :)

@dbu
Copy link
Contributor Author

dbu commented Apr 18, 2016

@nicolas-grekas fixed travis-ci. now only appveyor has a complaint left. can you help me here? it feels like this is not related to my change, right?

@nicolas-grekas
Copy link
Member

right!

@linaori
Copy link
Contributor

linaori commented Apr 18, 2016

@nicolas-grekas
Copy link
Member

See #18576

@dbu
Copy link
Contributor Author

dbu commented Apr 18, 2016 via email

@nicolas-grekas
Copy link
Member

Thank you @dbu.

@nicolas-grekas nicolas-grekas merged commit 0999326 into symfony:master Apr 18, 2016
nicolas-grekas added a commit that referenced this pull request Apr 18, 2016
This PR was merged into the 3.1-dev branch.

Discussion
----------

DX: better error message if factory class is empty

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

i was trying to debug this service configuration:

```xml
        <service id="repository.user" class="Doctrine\ORM\EntityRepository">
            <argument>%user_entity_class%</argument>
            <factory id="doctrine.orm.default_entity_manager" method="getRepository"/>
        </service>
```

Turns out i should have used `<factory service=...` instead of `<factory id=...` but the error message does not even tell which service is wrong. The same happens when only specifying the `method=` attribute. The current exception message tells:

    Cannot dump definition because of invalid class name ('')

With this change, the message at least tells the context, hopefully helping the developer debug the issue:

    Empty class for factory of service repository.user and no factory service specified

Commits
-------

0999326 better error message if factory class is empty
@xabbuh
Copy link
Member

xabbuh commented Apr 19, 2016

@nicolas-grekas Should we really treat this as a new feature instead of backporting this to older versions too? Imo this classifies as a kind of bugfix.

@fabpot
Copy link
Member

fabpot commented Apr 19, 2016

@xabbuh That's one of the points I wanted to clarify in my doc PR about where to merge such changes. In the past, we had 2 strategies about exception messages changes: merge them as bug fix or as new features.

People could rely on the error message in their code (even if this is clearly a bad practice), so changing the text in a patch release seems like a bad idea. Also, it does not fix any bug per se.

That being said, in this specific case, as the exception is during the container dump, I tend to agree with @xabbuh that it could have been merged as a bug fix.

Conclusion: Qualifying a code change as a bug fix or a new feature is really really hard :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DependencyInjection DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature Status: Needs Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants