Skip to content

Commit

Permalink
minor #54115 [DependencyInjection] Better error message when a param …
Browse files Browse the repository at this point in the history
…is not defined (lyrixx)

This PR was merged into the 7.1 branch.

Discussion
----------

[DependencyInjection] Better error message when a param is not defined

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        |
| License       | MIT

---

I'm migrating an application to flex, and bundles loading is important.
So let's enhance error reporting

## Before:

```
orpi /var/www/projects/frontend bin/console

In ParameterBag.php line 93:

  You have requested a non-existent parameter "orpi.ocom_domain". Did you mean one of these: "orpi_domain", "orpi_ocom_subdomain"?
```

## After

```
orpi /var/www/projects/frontend bin/console  -v

In ParameterBag.php line 93:

  [Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
  You have requested a non-existent parameter "orpi.ocom_domain" while loading extension "fos_http_cache". Did you mean one of these: "orpi_domain", "orpi_ocom_subdomain"?

```

Commits
-------

7080c6f [DependencyInjection] Better error message when a param is not defined
  • Loading branch information
nicolas-grekas committed Mar 14, 2024
2 parents 20d5453 + 7080c6f commit 682aff8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand Down Expand Up @@ -56,7 +57,14 @@ public function process(ContainerBuilder $container): void
BaseNode::setPlaceholderUniquePrefix($resolvingBag->getEnvPlaceholderUniquePrefix());
}
}
$config = $resolvingBag->resolveValue($config);

try {
$config = $resolvingBag->resolveValue($config);
} catch (ParameterNotFoundException $e) {
$e->setSourceExtensionName($name);

throw $e;
}

try {
$tmpContainer = new MergeExtensionConfigurationContainerBuilder($extension, $resolvingBag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
*/
class ParameterNotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface
{
private string $key;
private ?string $sourceId;
private ?string $sourceKey;
private array $alternatives;
private ?string $nonNestedAlternative;

/**
* @param string $key The requested parameter key
* @param string|null $sourceId The service id that references the non-existent parameter
Expand All @@ -34,14 +28,15 @@ class ParameterNotFoundException extends InvalidArgumentException implements Not
* @param string[] $alternatives Some parameter name alternatives
* @param string|null $nonNestedAlternative The alternative parameter name when the user expected dot notation for nested parameters
*/
public function __construct(string $key, ?string $sourceId = null, ?string $sourceKey = null, ?\Throwable $previous = null, array $alternatives = [], ?string $nonNestedAlternative = null)
{
$this->key = $key;
$this->sourceId = $sourceId;
$this->sourceKey = $sourceKey;
$this->alternatives = $alternatives;
$this->nonNestedAlternative = $nonNestedAlternative;

public function __construct(
private string $key,
private ?string $sourceId = null,
private ?string $sourceKey = null,
?\Throwable $previous = null,
private array $alternatives = [],
private ?string $nonNestedAlternative = null,
private ?string $sourceExtensionName = null,
) {
parent::__construct('', 0, $previous);

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

$this->updateRepr();
}

public function setSourceExtensionName(?string $sourceExtensionName): void
{
$this->sourceExtensionName = $sourceExtensionName;

$this->updateRepr();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
Expand Down Expand Up @@ -127,11 +128,32 @@ public function testThrowingExtensionsGetMergedBag()
$pass->process($container);
$this->fail('An exception should have been thrown.');
} catch (\Exception $e) {
$this->assertSame('here', $e->getMessage());
}

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

public function testMissingParameterIncludesExtension()
{
$container = new ContainerBuilder();
$container->registerExtension(new FooExtension());
$container->prependExtensionConfig('foo', [
'foo' => '%missing_parameter%',
]);

$pass = new MergeExtensionConfigurationPass();
try {
$pass = new MergeExtensionConfigurationPass();
$pass->process($container);
$this->fail('An exception should have been thrown.');
} catch (\Exception $e) {
$this->assertInstanceOf(ParameterNotFoundException::class, $e);
$this->assertSame('You have requested a non-existent parameter "missing_parameter" while loading extension "foo".', $e->getMessage());
}

}

public function testReuseEnvPlaceholderGeneratedByPreviousExtension()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -210,7 +232,7 @@ public function getConfiguration(array $config, ContainerBuilder $container): ?C

public function load(array $configs, ContainerBuilder $container): void
{
throw new \Exception();
throw new \Exception('here');
}
}

Expand Down

0 comments on commit 682aff8

Please sign in to comment.