Skip to content

Commit 35bdc20

Browse files
committed
[DependencyInjection] Better error message when a param is not defined
1 parent d5936a3 commit 35bdc20

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

Compiler/MergeExtensionConfigurationPass.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Config\Definition\BaseNode;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Exception\LogicException;
17+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1718
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1819
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
1920
use Symfony\Component\DependencyInjection\Extension\Extension;
@@ -56,7 +57,14 @@ public function process(ContainerBuilder $container): void
5657
BaseNode::setPlaceholderUniquePrefix($resolvingBag->getEnvPlaceholderUniquePrefix());
5758
}
5859
}
59-
$config = $resolvingBag->resolveValue($config);
60+
61+
try {
62+
$config = $resolvingBag->resolveValue($config);
63+
} catch (ParameterNotFoundException $e) {
64+
$e->setSourceExtensionName($name);
65+
66+
throw $e;
67+
}
6068

6169
try {
6270
$tmpContainer = new MergeExtensionConfigurationContainerBuilder($extension, $resolvingBag);

Exception/ParameterNotFoundException.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
*/
2121
class ParameterNotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface
2222
{
23-
private string $key;
24-
private ?string $sourceId;
25-
private ?string $sourceKey;
26-
private array $alternatives;
27-
private ?string $nonNestedAlternative;
28-
2923
/**
3024
* @param string $key The requested parameter key
3125
* @param string|null $sourceId The service id that references the non-existent parameter
@@ -34,14 +28,15 @@ class ParameterNotFoundException extends InvalidArgumentException implements Not
3428
* @param string[] $alternatives Some parameter name alternatives
3529
* @param string|null $nonNestedAlternative The alternative parameter name when the user expected dot notation for nested parameters
3630
*/
37-
public function __construct(string $key, ?string $sourceId = null, ?string $sourceKey = null, ?\Throwable $previous = null, array $alternatives = [], ?string $nonNestedAlternative = null)
38-
{
39-
$this->key = $key;
40-
$this->sourceId = $sourceId;
41-
$this->sourceKey = $sourceKey;
42-
$this->alternatives = $alternatives;
43-
$this->nonNestedAlternative = $nonNestedAlternative;
44-
31+
public function __construct(
32+
private string $key,
33+
private ?string $sourceId = null,
34+
private ?string $sourceKey = null,
35+
?\Throwable $previous = null,
36+
private array $alternatives = [],
37+
private ?string $nonNestedAlternative = null,
38+
private ?string $sourceExtensionName = null,
39+
) {
4540
parent::__construct('', 0, $previous);
4641

4742
$this->updateRepr();
@@ -53,6 +48,8 @@ public function updateRepr(): void
5348
$this->message = sprintf('The service "%s" has a dependency on a non-existent parameter "%s".', $this->sourceId, $this->key);
5449
} elseif (null !== $this->sourceKey) {
5550
$this->message = sprintf('The parameter "%s" has a dependency on a non-existent parameter "%s".', $this->sourceKey, $this->key);
51+
} elseif (null !== $this->sourceExtensionName) {
52+
$this->message = sprintf('You have requested a non-existent parameter "%s" while loading extension "%s".', $this->key, $this->sourceExtensionName);
5653
} elseif ('.' === ($this->key[0] ?? '')) {
5754
$this->message = sprintf('Parameter "%s" not found. It was probably deleted during the compilation of the container.', $this->key);
5855
} else {
@@ -99,4 +96,11 @@ public function setSourceKey(?string $sourceKey): void
9996

10097
$this->updateRepr();
10198
}
99+
100+
public function setSourceExtensionName(?string $sourceExtensionName): void
101+
{
102+
$this->sourceExtensionName = $sourceExtensionName;
103+
104+
$this->updateRepr();
105+
}
102106
}

Tests/Compiler/MergeExtensionConfigurationPassTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationContainerBuilder;
1919
use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationPass;
2020
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
2122
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2223
use Symfony\Component\DependencyInjection\Extension\Extension;
2324
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
@@ -127,11 +128,32 @@ public function testThrowingExtensionsGetMergedBag()
127128
$pass->process($container);
128129
$this->fail('An exception should have been thrown.');
129130
} catch (\Exception $e) {
131+
$this->assertSame('here', $e->getMessage());
130132
}
131133

132134
$this->assertSame(['FOO'], array_keys($container->getParameterBag()->getEnvPlaceholders()));
133135
}
134136

137+
public function testMissingParameterIncludesExtension()
138+
{
139+
$container = new ContainerBuilder();
140+
$container->registerExtension(new FooExtension());
141+
$container->prependExtensionConfig('foo', [
142+
'foo' => '%missing_parameter%',
143+
]);
144+
145+
$pass = new MergeExtensionConfigurationPass();
146+
try {
147+
$pass = new MergeExtensionConfigurationPass();
148+
$pass->process($container);
149+
$this->fail('An exception should have been thrown.');
150+
} catch (\Exception $e) {
151+
$this->assertInstanceOf(ParameterNotFoundException::class, $e);
152+
$this->assertSame('You have requested a non-existent parameter "missing_parameter" while loading extension "foo".', $e->getMessage());
153+
}
154+
155+
}
156+
135157
public function testReuseEnvPlaceholderGeneratedByPreviousExtension()
136158
{
137159
$container = new ContainerBuilder();
@@ -210,7 +232,7 @@ public function getConfiguration(array $config, ContainerBuilder $container): ?C
210232

211233
public function load(array $configs, ContainerBuilder $container): void
212234
{
213-
throw new \Exception();
235+
throw new \Exception('here');
214236
}
215237
}
216238

0 commit comments

Comments
 (0)