Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DI] Add runtime service exceptions to improve the error message when controller arguments cannot be injected#26627

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromnicolas-grekas:di-runtime-exception
Mar 28, 2018

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedMar 21, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#23997
LicenseMIT
Doc PR-

image

{
if (null !==$reference) {
switch ($reference->getInvalidBehavior()) {
case ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_DEFINITION:
Copy link
Member

Choose a reason for hiding this comment

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

I would break immediately instead of doing a fallthrough to the break in the next case.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

updated

@weaverryan
Copy link
Member

I've tested this out - it works really well. Wow :p. The only imperfection (for the main use-case of controller args) is that the message contains only the controllerclass, not the method. I can see technically why that is - it's tricky. But, is this possible by either passing some extra context information fromRegisterControllerArgumentLocatorsPass or by catching theRuntimeException inServiceValueResolver and adding more context with a new exception?

@nicolas-grekasnicolas-grekas changed the title[DI] Add ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_DEFINITION[DI] Add ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCEMar 22, 2018
@stof
Copy link
Member

what happens if multiple controllers are referencing the same broken typehint ?

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedMar 22, 2018
edited
Loading

PR embeds#26636 until it's merged.
@stof I was fixing that while you commented :)
@weaverryan I'm surprised you did not ask for the name of the argument also. So I added it ;)

if (ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE ===$invalidBehavior &&$valueinstanceof TypedReference) {
$e =newServiceNotFoundException($id = (string)$value,$this->currentId);

$this->container->register($id,$value->getType())
Copy link
Member

Choose a reason for hiding this comment

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

what if a next processed service was also trying to reference this service in the container, with a stricter behavior ? The service is now defined and will throw only at runtime.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

this cannot happen betweenResolveInvalidReferencesPass andCheckReferenceValidityPass, so that this case doesn't exist AFAIK

Copy link
Member

Choose a reason for hiding this comment

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

I mean, next services processed by this same compiler pass (this logic is in a loop over services)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Ok, got it. Better now?

$message =$this->createTypeNotFoundMessage($value,'it');

if (ContainerBuilder::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE ===$value->getInvalidBehavior()) {
$this->container->register((string)$value,$value->getType())
Copy link
Member

Choose a reason for hiding this comment

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

a next service referencing the same value in a stricter way would now use this service as it exists. This looks wrong to me.

Copy link
Member

Choose a reason for hiding this comment

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

and even if it also use RUNTIME_EXCEPTION_ON_INVALID_REFERENCE and so would be fine with a throwing definition, it would still need a different error message for the other service, and so adifferent throwing definition.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

The behavior is on the reference, not on the definition.DefinitionErrorExceptionPass will throw if any references exist that are notRUNTIME_EXCEPTION_ON_INVALID_REFERENCE.
Or I missed your point.

Copy link
Member

@stofstofMar 23, 2018
edited
Loading

Choose a reason for hiding this comment

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

yes, the behavior is on the reference. but when processing anext autowired service, the service will exist in the container (due to registering it here). But then, when loading thesecond service at runtime, this errored definition will be loaded, triggering an exception talking about thefirst service (as that's the one for which this service was registered).

The issue is that even though thebehavior is defined at the reference level, theerror is registered at the definition level (and so multiple references will share the same definition)

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

ok, fixed

Copy link
Member

Choose a reason for hiding this comment

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

please add tests covering this, to ensure that we keep getting warned with the right error message in the future, as this is a tricky case.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I added a comment instead.

$message =$this->createTypeNotFoundMessage($value,'it');

if (ContainerBuilder::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE ===$value->getInvalidBehavior()) {
$this->container->register($id =sprintf('errored.%s.%s',$this->currentId, (string)$value),$value->getType())
Copy link
Member

Choose a reason for hiding this comment

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

As we consider service ids starting with a underscore as reserved since 4.0 (at least in the YAML file loader, not sure about the XML one and the PHP DSL), I suggest using_errored. as prefix instead to reduce the likeliness of conflicts.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

fixed

@nicolas-grekas
Copy link
MemberAuthor

Comments addressed.

@nicolas-grekasnicolas-grekas changed the title[DI] Add ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE[DI] Add runtime service exceptions to improve the error message when controller arguments cannot be injectedMar 26, 2018
@fabpot
Copy link
Member

Thank you@nicolas-grekas.

@fabpotfabpot merged commit9e8e063 intosymfony:masterMar 28, 2018
fabpot added a commit that referenced this pull requestMar 28, 2018
…or message when controller arguments cannot be injected (nicolas-grekas)This PR was merged into the 4.1-dev branch.Discussion----------[DI] Add runtime service exceptions to improve the error message when controller arguments cannot be injected| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#23997| License       | MIT| Doc PR        | -![image](https://user-images.githubusercontent.com/243674/37775694-e5c9814c-2de3-11e8-8290-8fd05086da28.png)Commits-------9e8e063 [DI] Add ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE
@nicolas-grekasnicolas-grekas deleted the di-runtime-exception branchApril 2, 2018 12:07
@fabpotfabpot mentioned this pull requestMay 7, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@stofstofstof approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

4.1

Development

Successfully merging this pull request may close these issues.

5 participants

@nicolas-grekas@weaverryan@stof@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp