From 6894e03e16fc628bc4b91ed051c31ad2a8c30f6e Mon Sep 17 00:00:00 2001 From: Mathieu Lemoine Date: Mon, 2 May 2016 11:12:26 -0400 Subject: [PATCH] Make failed autowiring error messages more explicit --- .../DependencyInjection/Compiler/AutowirePass.php | 11 +++++++++-- .../Tests/Compiler/AutowirePassTest.php | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index 784e084205..8e1b06e0ff 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -271,7 +271,7 @@ class AutowirePass implements CompilerPassInterface if (!$typeHint->isInstantiable()) { $classOrInterface = $typeHint->isInterface() ? 'interface' : 'class'; - 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)); + 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)); } $argumentId = sprintf('autowired.%s', $typeHint->name); @@ -280,7 +280,14 @@ class AutowirePass implements CompilerPassInterface $argumentDefinition->setPublic(false); $this->populateAvailableType($argumentId, $argumentDefinition); - $this->completeDefinition($argumentId, $argumentDefinition); + + try { + $this->completeDefinition($argumentId, $argumentDefinition); + } catch (\RuntimeException $e) { + $classOrInterface = $typeHint->isInterface() ? 'interface' : 'class'; + $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); + throw new RuntimeException($message, 0, $e); + } return new Reference($argumentId); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 1644180aec..8edca3b1cc 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -155,7 +155,7 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase /** * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException - * @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" for the service "a". No services were found matching this interface. + * @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. */ public function testTypeNotGuessableNoServicesFound() {