Skip to content

Commit af5bbba

Browse files
committed
feature #19892 [DI] Add corresponding service id in some exception messages (nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [DI] Add corresponding service id in some exception messages | Q | A | ------------- | --- | Branch? | master | New feature? | yes | Tests pass? | yes | License | MIT This already helped me figure out where was my mistake :) Commits ------- d739f8d [DI] Add corresponding service id in some exception messages
2 parents 6b3755d + 9345d8c commit af5bbba

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Compiler/ResolveDefinitionTemplatesPass.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Definition;
1515
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
1718
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1819

1920
/**
@@ -95,9 +96,22 @@ private function resolveArguments(ContainerBuilder $container, array $arguments,
9596
* @throws \RuntimeException When the definition is invalid
9697
*/
9798
private function resolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition)
99+
{
100+
try {
101+
return $this->doResolveDefinition($container, $definition);
102+
} catch (ExceptionInterface $e) {
103+
$r = new \ReflectionProperty($e, 'message');
104+
$r->setAccessible(true);
105+
$r->setValue($e, sprintf('Service "%s": %s', $this->currentId, $e->getMessage()));
106+
107+
throw $e;
108+
}
109+
}
110+
111+
private function doResolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition)
98112
{
99113
if (!$container->has($parent = $definition->getParent())) {
100-
throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $this->currentId));
114+
throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
101115
}
102116

103117
$parentDef = $container->findDefinition($parent);

Definition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function getFactory()
9393
public function setDecoratedService($id, $renamedId = null, $priority = 0)
9494
{
9595
if ($renamedId && $id == $renamedId) {
96-
throw new \InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
96+
throw new InvalidArgumentException(sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
9797
}
9898

9999
if (null === $id) {

0 commit comments

Comments
 (0)