Skip to content

[DependencyInjection] Dump and read the first error of service for XML and YAML format #34928

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Compiler\RemoveAbstractDefinitionsPass;
use Symfony\Component\DependencyInjection\Compiler\RemovePrivateAliasesPass;
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
use Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
Expand Down Expand Up @@ -81,6 +87,16 @@ private function getContainerBuilder(): ContainerBuilder
$refl = new \ReflectionProperty($parameterBag, 'resolved');
$refl->setAccessible(true);
$refl->setValue($parameterBag, true);

// To keep in sync with the default removing passes of PassConfig minus DefinitionErrorExceptionPass
$container->getCompilerPassConfig()->setRemovingPasses([
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Setting everything like this seems like the easier and faster option.

new RemovePrivateAliasesPass(),
new ReplaceAliasByActualDefinitionPass(),
new RemoveAbstractDefinitionsPass(),
new RemoveUnusedDefinitionsPass(),
new InlineServiceDefinitionsPass(new AnalyzeServiceReferencesPass()),
new AnalyzeServiceReferencesPass(),
]);
}

return $this->containerBuilder = $container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ protected function processValue($value, $isRoot = false)
}

// only show the first error so the user can focus on it
$errors = $value->getErrors();
$message = reset($errors);
$message = $value->getFirstError();

throw new RuntimeException($message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@ private function doGet(string $id, int $invalidBehavior = ContainerInterface::EX
throw $e;
}

if ($definition->hasErrors() && $e = $definition->getErrors()) {
throw new RuntimeException(reset($e));
if ($e = $definition->getFirstError()) {
throw new RuntimeException($e);
}

if ($isConstructorArgument) {
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -937,4 +937,26 @@ public function hasErrors(): bool
{
return (bool) $this->errors;
}

/**
* @internal
*/
public function getFirstError(): ?string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas This new internal method avoid to potentially compute several error messages, to finally only use the first one in all of our usage.

{
if (!$this->hasErrors()) {
return null;
}

$error = $this->errors[0];

if ($error instanceof \Closure) {
return (string) $error();
}

if (!\is_string($error)) {
return (string) $error;
}

return $error;
}
}
10 changes: 5 additions & 5 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1667,7 +1667,7 @@ private function dumpValue($value, bool $interpolate = true): string
continue;
}
$definition = $this->container->findDefinition($id = (string) $v);
$load = !($definition->hasErrors() && $e = $definition->getErrors()) ? $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) : reset($e);
$load = !($e = $definition->getFirstError()) ? $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) : $e;
$serviceMap .= sprintf("\n %s => [%s, %s, %s, %s],",
$this->export($k),
$this->export($definition->isShared() ? ($definition->isPublic() ? 'services' : 'privates') : false),
Expand All @@ -1686,10 +1686,10 @@ private function dumpValue($value, bool $interpolate = true): string
list($this->definitionVariables, $this->referenceVariables) = $scope;
}
} elseif ($value instanceof Definition) {
if ($value->hasErrors() && $e = $value->getErrors()) {
if ($e = $value->getFirstError()) {
$this->addThrow = true;

return sprintf('$this->throw(%s)', $this->export(reset($e)));
return sprintf('$this->throw(%s)', $this->export($e));
}
if (null !== $this->definitionVariables && $this->definitionVariables->contains($value)) {
return $this->dumpValue($this->definitionVariables[$value], $interpolate);
Expand Down Expand Up @@ -1800,10 +1800,10 @@ private function getServiceCall(string $id, Reference $reference = null): string
return $code;
}
} elseif ($this->isTrivialInstance($definition)) {
if ($definition->hasErrors() && $e = $definition->getErrors()) {
if ($e = $definition->getFirstError()) {
$this->addThrow = true;

return sprintf('$this->throw(%s)', $this->export(reset($e)));
return sprintf('$this->throw(%s)', $this->export($e));
}
$code = $this->addNewInstance($definition, '', $id);
if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ private function addService(Definition $definition, ?string $id, \DOMElement $pa
$service->appendChild($configurator);
}

if ($e = $definition->getFirstError()) {
$service->setAttribute('error', $e);
}

$parent->appendChild($service);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ private function addService(string $id, Definition $definition): string
$code .= sprintf(" configurator: %s\n", $this->dumper->dump($this->dumpCallable($callable), 0));
}

if ($e = $definition->getFirstError()) {
$code .= sprintf(" error: %s\n", $this->dumper->dump($e));
}

return $code;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ private function parseDefinition(\DOMElement $service, string $file, array $defa
$definition->setDecoratedService($decorates, $renameId, $priority, $invalidBehavior);
}

if ($e = $service->getAttribute('error')) {
$definition->addError($e);
}

return $definition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class YamlFileLoader extends FileLoader
'autowire' => 'autowire',
'autoconfigure' => 'autoconfigure',
'bind' => 'bind',
'error' => 'error',
];

private static $prototypeKeywords = [
Expand Down Expand Up @@ -602,6 +603,10 @@ private function parseDefinition(string $id, $service, string $file, array $defa
throw new InvalidArgumentException(sprintf('A "resource" attribute must be set when the "namespace" attribute is set for service "%s" in %s. Check your YAML syntax.', $id, $file));
}

if (isset($service['error'])) {
$definition->addError($service['error']);
}

if (\array_key_exists('resource', $service)) {
if (!\is_string($service['resource'])) {
throw new InvalidArgumentException(sprintf('A "resource" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<xsd:attribute name="decoration-priority" type="xsd:integer" />
<xsd:attribute name="autowire" type="boolean" />
<xsd:attribute name="autoconfigure" type="boolean" />
<xsd:attribute name="error" type="xsd:string" />
Copy link
Contributor Author

@fancyweb fancyweb Dec 10, 2019

Choose a reason for hiding this comment

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

Reading the error is good for us, but that also mean it can be defined by users with this new attribute (preferred to keep things simple with an attribute btw). We open this new possibility. I'm not sure it's good 🤔 That makes it a feature too? That means it can't really go on 4.4 and that we can't fix the linked issue like this? I need some input on all of this, thanks.

</xsd:complexType>

<xsd:complexType name="instanceof">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<service id="runtime_error" class="stdClass" public="true">
<argument type="service" id="errored_definition"/>
</service>
<service id="errored_definition" class="stdClass"/>
<service id="errored_definition" class="stdClass" error="Service &quot;errored_definition&quot; is broken."/>
<service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/>
<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/>
<service id="alias_for_foo" alias="foo" public="true"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,4 @@ services:
public: true
errored_definition:
class: stdClass
error: Service "errored_definition" is broken.