8000 [DX] Improve exception message when AbstractController::getParameter fails by curry684 · Pull Request #27443 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DX] Improve exception message when AbstractController::getParameter fails #27443

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
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Psr\Container\ContainerInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
Expand Down Expand Up @@ -64,7 +65,14 @@ public function setContainer(ContainerInterface $container)
protected function getParameter(string $name)
{
if (!$this->container->has('parameter_bag')) {
throw new \LogicException('The "parameter_bag" service is not 8000 available. Try running "composer require dependency-injection:^4.1"');
$message = 'The "parameter_bag" service could not be located. ';
if (!interface_exists(ContainerBagInterface::class)) {
$message .= 'Upgrade symfony/dependency-injection to at least 4.1.0 to have it injected automatically.';
Copy link
Member

Choose a reason for hiding this comment

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

suggesting to install/upgrade symfony/dependency-injection does not seem relevant as framework-bundle requires it ^4.1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lol, hadn't bothered to doublecheck that because it was the original message and I assumed it to have a reason.

} else {
$message .= 'Ensure that the controller is either set to be autoconfigured or wired manually to inject it.';
}

throw new ServiceNotFoundException('parameter_bag', null, null, array(), $message);
}

return $this->container->get('parameter_bag')->get($name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,32 @@ public function testSubscribedServices()

public function testGetParameter()
{
if (!class_exists(ContainerBag::class)) {
$this->markTestSkipped('ContainerBag class does not exist');
}

$container = new Container(new FrozenParameterBag(array('foo' => 'bar')));
$container->set('parameter_bag', new ContainerBag($container));

$controller = $this->createController();
$controller->setContainer($container);

if (!class_exists(ContainerBag::class)) {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
} else {
$container->set('parameter_bag', new ContainerBag($container));
}

$this->assertSame('bar', $controller->getParameter('foo'));
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
* @expectedExceptionMessage The "parameter_bag" service could not be located.
*/
public function testMissingParameterBag()
{
$container = new Container();

$controller = $this->createController();
$controller->setContainer($container);

$controller->getParameter('foo');
}
}

class TestAbstractController extends AbstractController
Expand Down
0