8000 [DX][DI] Make failed autowiring error messages more explicit by lemoinem · Pull Request #18691 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DX][DI] Make failed autowiring error messages more explicit #18691

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
May 3, 2016

Conversation

lemoinem
Copy link
Contributor
@lemoinem lemoinem commented May 2, 2016
Q A
Branch? 2.8
Bug fix? no (better DX integration)
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #18658
License MIT
Doc PR N/A

This is the PR improving the auto wiring error messages.
Two errors messages have augmented:

If a type-hint does not match any existing type and a service for this type cannot be automatically created, the error message now says so, instead of simply saying the type cannot be autowired.

If a type-hint matches multiple services and none of them provides an autowiringType for it, the error message now says so and list the candidate services, instead of simply saying the type cannot be autowired.

@lemoinem lemoinem force-pushed the fix/autowiring/exception-message branch from 891dd3c to c8cf523 Compare May 2, 2016 15:18
@javiereguiluz javiereguiluz added DependencyInjection DX DX = Developer eXperience (anything that improves the experience of using Symfony) labels May 2, 2016
if (isset($this->notGuessableTypes[$typeHint->name]) || !$typeHint->isInstantiable()) {
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s".', $typeHint->name, $id));
if (isset($this->notGuessableTypes[$typeHint->name])) {
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Several services implementing this type has been declared: "%s".', $typeHint->name, $id, implode('", "', $this->types[$typeHint->name])));
Copy link
Member

Choose a reason for hiding this comment

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

Several services ... has been declared -> Several services ... have been declared

@lemoinem lemoinem force-pushed the fix/autowiring/exception-message branch from c8cf523 to 08cd5c7 Compare May 2, 2016 15:28
@lemoinem lemoinem changed the title [DI][DX] Make failed autowiring error messages more explicit [DX][DI] Make failed autowiring error messages more explicit May 2, 2016
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Several services implementing this type have been declared: "%s".', $typeHint->name, $id, implode('", "', $this->types[$typeHint->name])));
}

$NoAvailableDefinitionMessage = sprintf('Unable to autowire argument of type "%s" for the service "%s". This type cannot be instantiated automatically and no service implementing this type is declared.', $typeHint->name, $id);
Copy link
Member

Choose a reason for hiding this comment

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

the leading n should be lowercase in the variable name

@lemoinem lemoinem force-pushed the fix/autowiring/exception-message branch from 08cd5c7 to 2ac81f9 Compare May 2, 2016 15:29
@lemoinem
Copy link
Contributor Author
lemoinem commented May 2, 2016

Thanks javiereguiluz and stof for the code/grammar review, it's been fixed.
@xabbuh, @dunglas : The tests are all 💚 (: What do you think about the PR?

@stof
Copy link
Member
stof commented May 3, 2016

👍

Status: reviewed

8000
@fabpot
Copy link
Member
fabpot commented May 3, 2016

Thank you @lemoinem.

@fabpot fabpot merged commit 2ac81f9 into symfony:2.8 May 3, 2016
fabpot added a commit that referenced this pull request May 3, 2016
…icit (lemoinem)

This PR was merged into the 2.8 branch.

Discussion
----------

[DX][DI] Make failed autowiring error messages more explicit

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | no (better DX integration)
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18658
| License       | MIT
| Doc PR        | N/A

This is the PR improving the auto wiring error messages.
Two errors messages have augmented:

If a type-hint does not match any existing type and a service for this type cannot be automatically created, the error message now says so, instead of simply saying the type cannot be autowired.

If a type-hint matches multiple services and none of them provides an  autowiringType for it, the error message now says so and list the candidate services, instead of simply saying the type cannot be autowired.

Commits
-------

2ac81f9 Make failed autowiring error messages more explicit
@lemoinem lemoinem deleted the fix/autowiring/exception-message branch May 3, 2016 18:27
@nicolas-grekas
Copy link
Member

@lemoinem I did not merge you work here into master because it caused conflicts and I did not know how to fix it. Can you please open a new PR against master?

@lemoinem
Copy link
Contributor Author

@nicolas-grekas Got it, I will check this out and provide a new PR ASAP. Did you have any issue applying the PR on 3.0? Or should I worry only about master/3.1?

fabpot added a commit that referenced this pull request May 13, 2016
…nem)

This PR was merged into the 3.1-dev branch.

Discussion
----------

Make failed autowiring error messages more explicit

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no (better DX integration)
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18658
| License       | MIT
| Doc PR        | N/A

@nicolas-grekas This is a port from #18691 (which was for **2.8**).
It looks like the original PR has been already mostly implemented in **master** by @weaverryan (in #17877).

IMO, this PR is still improving two use cases:

  * In case a class cannot be autowired because no service can be found AND a service cannot be registered automatically, the error message now mentions both conditions (instead of simply saying no service has been found)
  * In case a class with no service attached to it can be automatically instantiated but cannot be autowired because of further problems, an exception mentioning this is now thrown instead of the lower level exception, making the situation easier to debug. (The lower level exception is still available through `getPrevious()`)

Hence, I still think this PR provides useful additional DX features and is worth to be merged.

Commits
-------

6894e03 Make failed autowiring error messages more explicit
nicolas-grekas added a commit that referenced this pull request May 19, 2016
…e future friendly (lemoinem)

This PR was merged into the 2.8 branch.

Discussion
----------

[DX][DependencyInjection] Make Autowiring exceptions more future friendly

| Q             | A
| ------------- | ---
| Branch?       | 2.8, 3.0
| Bug fix?      | no (forward compatibility)
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

This is a follow-up on #18766 (3.1, master) and #18691 (2.8, 3.0).

This PR changes the exception messages to the ones introduced in #18766 and #17877. I also unified the tests so they are exactly the same.

This way the error messages will be the same in 2.8 onward.

Commits
-------

834f550 [DX][DI] Make Autowiring exceptions more future friendly
nicolas-grekas added a commit that referenced this pull request Feb 14, 2017
This PR was merged into the 3.2 branch.

Discussion
----------

[DI] Align AutowirePass with 2.8

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

Follow up a less than ideal [merge conflict resolution](#18691 (comment)), found while reviewing AutowirePass in 2.8 and 3.2 and wondering about the delta. Should reduce future merge conflicts and help history tracking.

Commits
-------

dd5236a [DI] Align AutowirePass with 2.8
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull request Feb 14, 2017
This PR was merged into the 3.2 branch.

Discussion
----------

[DI] Align AutowirePass with 2.8

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

Follow up a less than ideal [merge conflict resolution](symfony/symfony#18691 (comment)), found while reviewing AutowirePass in 2.8 and 3.2 and wondering about the delta. Should reduce future merge conflicts and help history tracking.

Commits
-------

dd5236a [DI] Align AutowirePass with 2.8
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) Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0