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

Skip to content

Adding Definition::addError() and a compiler pass to throw errors as exceptions #24290

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 6 commits into from

Conversation

weaverryan
Copy link
Member

@weaverryan weaverryan commented Sep 22, 2017

Q A
Branch? 3.4
Bug fix? yes & no
New feature? yes
BC breaks? no
Deprecations? yes (very minor)
Tests pass? yes
Fixed tickets #23606
License MIT
Doc PR Not needed

Hi guys!

Very simple: when there is an error with a Definition, we can now call Definition::addError() instead of throwing an exception. Then, a new compiler pass (after removal) actually throws an exception. The advantage is that we can avoid throwing exceptions for services that are ultimately removed from the container. That's important for auto-registration, where we commonly register all services in src/... but then many of them are removed later.

A few interesting notes:

  • We can probably convert more things from exceptions to Definition::addError(). I've only converted autowiring errors and things in CheckArgumentsValidityPass (that was necessary because it was throwing exceptions in some cases due to autowiring failing... which was the true error)
  • Definition can hold multiple errors, but I'm only showing the first error in the exception message. The reason is clarity: I think usually the first error is the most (or only) important. But having Definition::addError() avoids the possibility of a later error overriding an earlier one

Cheers!

@javiereguiluz javiereguiluz changed the title Adding Definition::addError() and a compiler pass to throw errors as Adding Definition::addError() and a compiler pass to throw errors as exceptions Sep 22, 2017
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.

nice :)

@@ -11,6 +11,8 @@

namespace Symfony\Component\DependencyInjection\Compiler;

@trigger_error('The '.__NAMESPACE__.'\AutowireExceptionPass class is deprecated since version 3.4 and will be removed in 4.0. Use the DefinitionErrorExceptionPass class instead.', E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

missing annotation on the class

*/
public function __construct($throwOnAutowireException = true)
{
$this->throwOnAutowiringException = $throwOnAutowireException;
}

/**
* @deprecated
Copy link
Member

Choose a reason for hiding this comment

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

since version 3.4, to be removed in 4.0

@@ -35,10 +42,19 @@ protected function processValue($value, $isRoot = false)
foreach ($value->getArguments() as $k => $v) {
if ($k !== $i++) {
if (!is_int($k)) {
throw new RuntimeException(sprintf('Invalid constructor argument for service "%s": integer expected but found string "%s". Check your service definition.', $this->currentId, $k));
$value->addError(sprintf('Invalid constructor argument for service "%s": integer expected but found string "%s". Check your service definition.', $this->currentId, $k));
Copy link
Member

Choose a reason for hiding this comment

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

dedup the msg with a var? (same below)

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Sep 23, 2017
@weaverryan
Copy link
Member Author

Good suggestions! They're all handled :)

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.

last comments :)

@@ -1090,7 +1093,7 @@ public function setFoo(Foo $foo)
// should be called
}

/** @inheritdoc*/
/** {@inheritdoc}*/
Copy link
Member

Choose a reason for hiding this comment

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

should be reverted, a fabbot false-positive

unrelated: InlineServiceDefinitionsPass::getInlinedServiceIds() should be deprecated also

use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Throws autowire exceptions from AutowirePass for definitions that still exist.
*
* @deprecated AutowireExceptionPass is deprecated since version 3.4 and will be removed in 4.0.
Copy link
Member

Choose a reason for hiding this comment

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

@deprecated since version 3.4, will be removed in 4.0.

@weaverryan
Copy link
Member Author

Tests pass, fabbot failure is false! Should be ready to go :)

@nicolas-grekas
Copy link
Member

Thank you @weaverryan.

nicolas-grekas added a commit that referenced this pull request Sep 25, 2017
…hrow errors as exceptions (weaverryan)

This PR was squashed before being merged into the 3.4 branch (closes #24290).

Discussion
----------

Adding Definition::addError() and a compiler pass to throw errors as exceptions

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes & no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes (very minor)
| Tests pass?   | yes
| Fixed tickets | #23606
| License       | MIT
| Doc PR        | Not needed

Hi guys!

Very simple: when there is an error with a Definition, we can now call `Definition::addError()` instead of throwing an exception. Then, a new compiler pass (after removal) actually throws an exception. The advantage is that we can avoid throwing exceptions for services that are ultimately removed from the container. That's important for auto-registration, where we commonly register all services in `src/`... but then many of them are removed later.

A few interesting notes:
- We can probably convert more things from exceptions to `Definition::addError()`. I've only converted autowiring errors and things in `CheckArgumentsValidityPass` (that was necessary because it was throwing exceptions in some cases due to autowiring failing... which was the true error)
- `Definition` can hold multiple errors, but I'm only showing the first error in the exception message. The reason is clarity: I think usually the first error is the most (or only) important. But having `Definition::addError()` avoids the possibility of a later error overriding an earlier one

Cheers!

Commits
-------

a85b37a Adding Definition::addError() and a compiler pass to throw errors as exceptions
@weaverryan weaverryan deleted the definition-add-error branch September 25, 2017 12:51
nicolas-grekas added a commit that referenced this pull request Oct 2, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Fix missing CHANGELOG update + typo

| Q             | A
| ------------- | ---
| Branch?       | 3.4 <!-- see comment below -->
| Bug fix?      | no
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #24290 <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

Commits
-------

f9aeee5 [DI] Fix missing CHANGELOG update + typo
nicolas-grekas added a commit that referenced this pull request Oct 2, 2017
This PR was merged into the 4.0-dev branch.

Discussion
----------

[DI] Remove AutowireExceptionPass

| Q             | A
| ------------- | ---
| Branch?       | master <!-- see comment below -->
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | yes
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #24290 <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

Commits
-------

2ada558 [DI] Remove AutowireExceptionPass
This was referenced Oct 18, 2017
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.

3 participants