Skip to content

Commit

Permalink
Resolver::completeException() appends entity when type is not known
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 6, 2020
1 parent 5ed8cfa commit 5db4398
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
30 changes: 18 additions & 12 deletions src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,26 @@ private function completeException(\Exception $e, Definition $def): ServiceCreat
{
if ($e instanceof ServiceCreationException && Strings::startsWith($e->getMessage(), "Service '")) {
return $e;
}

$name = $def->getName();
$type = $def->getType();
if ($name && !ctype_digit($name)) {
$message = "Service '$name'" . ($type ? " (type of $type)" : '') . ': ';
} elseif ($type) {
$message = "Service of type $type: ";
} elseif ($def instanceof Definitions\ServiceDefinition && $def->getEntity()) {
$message = 'Service (' . $this->entityToString($def->getEntity()) . '): ';
} else {
$name = $def->getName();
$type = $def->getType();
if (!$type) {
$message = "Service '$name': " . $e->getMessage();
} elseif (!$name || ctype_digit($name)) {
$message = "Service of type $type: " . str_replace("$type::", '', $e->getMessage());
} else {
$message = "Service '$name' (type of $type): " . str_replace("$type::", '', $e->getMessage());
}
return $e instanceof ServiceCreationException
? $e->setMessage($message)
: new ServiceCreationException($message, 0, $e);
$message = '';
}
$message .= $type
? str_replace("$type::", '', $e->getMessage())
: $e->getMessage();

return $e instanceof ServiceCreationException
? $e->setMessage($message)
: new ServiceCreationException($message, 0, $e);
}


Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Compiler.missingDefinition.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Assert::throws(function () {
services:
-
');
}, Nette\InvalidStateException::class, "Service '01': Factory and type are missing in definition of service.");
}, Nette\InvalidStateException::class, 'Factory and type are missing in definition of service.');


Assert::throws(function () {
Expand Down
7 changes: 7 additions & 0 deletions tests/DI/ContainerBuilder.factory.error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ Assert::exception(function () {
}, Nette\InvalidArgumentException::class, "Service 'one': Class or interface 'X' not found.");


Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition(null)->setFactory('Unknown');
$builder->complete();
}, Nette\DI\ServiceCreationException::class, 'Service (Unknown::__construct()): Class Unknown not found.');


Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('one')->setFactory('@two');
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Definitions.AccessorDefinition.resolve.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Assert::exception(function () {
$def = new AccessorDefinition;
$resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder);
$resolver->resolveDefinition($def);
}, Nette\DI\ServiceCreationException::class, "Service '': Type of service is unknown.");
}, Nette\DI\ServiceCreationException::class, 'Type of service is unknown.');


Assert::exception(function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Definitions.FactoryDefinition.resolve.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Assert::exception(function () {
$def = new FactoryDefinition;
$resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder);
$resolver->resolveDefinition($def);
}, Nette\DI\ServiceCreationException::class, "Service '': Type is missing in definition of service.");
}, Nette\DI\ServiceCreationException::class, 'Type is missing in definition of service.');


Assert::exception(function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Definitions.ImportedDefinition.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Assert::exception(function () {
$def = new ImportedDefinition;
$resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder);
$resolver->resolveDefinition($def);
}, Nette\DI\ServiceCreationException::class, "Service '': Type of service is unknown.");
}, Nette\DI\ServiceCreationException::class, 'Type of service is unknown.');


test('', function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/DI/Definitions.LocatorDefinition.resolve.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Assert::exception(function () {
$def = new LocatorDefinition;
$resolver = new Nette\DI\Resolver(new Nette\DI\ContainerBuilder);
$resolver->resolveDefinition($def);
}, Nette\DI\ServiceCreationException::class, "Service '': Type of service is unknown.");
}, Nette\DI\ServiceCreationException::class, 'Type of service is unknown.');


test('', function () {
Expand Down

0 comments on commit 5db4398

Please sign in to comment.