8000 bug #18766 Make failed autowiring error messages more explicit (lemoi… · symfony/symfony@219b050 · GitHub
[go: up one dir, main page]

Skip to content

Commit 219b050

Browse files
committed
bug #18766 Make failed autowiring error messages more explicit (lemoinem)
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
2 parents b5b141c + 6894e03 commit 219b050

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
271271

272272
if (!$typeHint->isInstantiable()) {
273273
$classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
274-
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". No services were found matching this %s.', $typeHint->name, $id, $classOrInterface));
274+
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". No services were found matching this %s and it cannot be auto-registered.', $typeHint->name, $id, $classOrInterface));
275275
}
276276

277277
$argumentId = sprintf('autowired.%s', $typeHint->name);
@@ -280,7 +280,14 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
280280
$argumentDefinition->setPublic(false);
281281

282282
$this->populateAvailableType($argumentId, $argumentDefinition);
283-
$this->completeDefinition($argumentId, $argumentDefinition);
283+
284+
try {
285+
$this->completeDefinition($argumentId, $argumentDefinition);
286+
} catch (\RuntimeException $e) {
287+
$classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
288+
$message = sprintf('Unable to autowire argument of type "%s" for the service "%s". No services were found matching this %s and it cannot be auto-registered.', $typeHint->name, $id, $classOrInterface);
289+
throw new RuntimeException($message, 0, $e);
290+
}
284291

285292
return new Reference($argumentId);
286293
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function testTypeNotGuessableWithSubclass()
155155

156156
/**
157157
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
158-
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" for the service "a". No services were found matching this interface.
158+
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" for the service "a". No services were found matching this interface and it cannot be auto-registered.
159159
*/
160160
public function testTypeNotGuessableNoServicesFound()
161161
{

0 commit comments

Comments
 (0)
0