Skip to content

[DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder #17719

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\ResourceInterface;
Expand Down Expand Up @@ -415,9 +417,9 @@ public function has($id)
*
* @return object The associated service
*
* @throws InvalidArgumentException when no definitions are available
* @throws InactiveScopeException when the current scope is not active
* @throws LogicException when a circular dependency is detected
* @throws InvalidArgumentException when no definitions are available
* @throws ServiceCircularReferenceException When a circular reference is detected
* @throws ServiceNotFoundException When the service is not defined
* @throws \Exception
*
* @see Reference
Expand All @@ -440,7 +442,8 @@ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INV

try {
$definition = $this->getDefinition($id);
} catch (InvalidArgumentException $e) {
} catch (ServiceNotFoundException $e) {

Copy link
Member

Choose a reason for hiding this comment

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

This empty line should be removed.

if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return;
}
Expand Down Expand Up @@ -790,14 +793,14 @@ public function hasDefinition($id)
*
* @return Definition A Definition instance
*
* @throws InvalidArgumentException if the service definition does not exist
* @throws ServiceNotFoundException if the service definition does not exist
*/
public function getDefinition($id)
{
$id = strtolower($id);

if (!array_key_exists($id, $this->definitions)) {
throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
throw new ServiceNotFoundException($id);
}

return $this->definitions[$id];
Expand All @@ -812,7 +815,7 @@ public function getDefinition($id)
*
* @return Definition A Definition instance
*
* @throws InvalidArgumentException if the service definition does not exist
* @throws ServiceNotFoundException if the service definition does not exist
*/
public function findDefinition($id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -50,9 +52,9 @@ public function testDefinitions()

try {
$builder->getDefinition('baz');
$this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
} catch (\InvalidArgumentException $e) {
$this->assertEquals('The service definition "baz" does not exist.', $e->getMessage(), '->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
$this->fail('->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
} catch (ServiceNotFoundException $e) {
$this->assertEquals('You have requested a non-existent service "baz".', $e->getMessage(), '->getDefinition() throws a ServiceNotFoundException if the service definition does not exist');
}
}

Expand All @@ -79,9 +81,9 @@ public function testGet()
$builder = new ContainerBuilder();
try {
$builder->get('foo');
$this->fail('->get() throws an InvalidArgumentException if the service does not exist');
} catch (\InvalidArgumentException $e) {
$this->assertEquals('The service definition "foo" does not exist.', $e->getMessage(), '->get() throws an InvalidArgumentException if the service does not exist');
$this->fail('->get() throws a ServiceNotFoundException if the service does not exist');
} catch (ServiceNotFoundException $e) {
$this->assertEquals('You have requested a non-existent service "foo".', $e->getMessage(), '->get() throws a ServiceNotFoundException if the service does not exist');
}

$this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument');
Expand Down