From 4c651dc181f56158d71e61ec5d28663ba86d84b6 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 4 Nov 2020 16:08:46 +0100 Subject: [PATCH] Resolver::completeException() appends entity when type is not known --- src/DI/Resolver.php | 30 +++++++++++-------- tests/DI/Compiler.missingDefinition.phpt | 2 +- ...efinitions.AccessorDefinition.resolve.phpt | 2 +- ...Definitions.FactoryDefinition.resolve.phpt | 2 +- tests/DI/Definitions.ImportedDefinition.phpt | 2 +- ...Definitions.LocatorDefinition.resolve.phpt | 2 +- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/DI/Resolver.php b/src/DI/Resolver.php index c93f768ba..cde266fee 100644 --- a/src/DI/Resolver.php +++ b/src/DI/Resolver.php @@ -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); } diff --git a/tests/DI/Compiler.missingDefinition.phpt b/tests/DI/Compiler.missingDefinition.phpt index 2697bd89f..e7630a781 100644 --- a/tests/DI/Compiler.missingDefinition.phpt +++ b/tests/DI/Compiler.missingDefinition.phpt @@ -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 () { diff --git a/tests/DI/Definitions.AccessorDefinition.resolve.phpt b/tests/DI/Definitions.AccessorDefinition.resolve.phpt index cbda9b526..9da6668ec 100644 --- a/tests/DI/Definitions.AccessorDefinition.resolve.phpt +++ b/tests/DI/Definitions.AccessorDefinition.resolve.phpt @@ -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 () { diff --git a/tests/DI/Definitions.FactoryDefinition.resolve.phpt b/tests/DI/Definitions.FactoryDefinition.resolve.phpt index d30e996a1..53dd55d1f 100644 --- a/tests/DI/Definitions.FactoryDefinition.resolve.phpt +++ b/tests/DI/Definitions.FactoryDefinition.resolve.phpt @@ -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 () { diff --git a/tests/DI/Definitions.ImportedDefinition.phpt b/tests/DI/Definitions.ImportedDefinition.phpt index 5a26a7911..442a18c9d 100644 --- a/tests/DI/Definitions.ImportedDefinition.phpt +++ b/tests/DI/Definitions.ImportedDefinition.phpt @@ -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 () { diff --git a/tests/DI/Definitions.LocatorDefinition.resolve.phpt b/tests/DI/Definitions.LocatorDefinition.resolve.phpt index 7e92c45fb..aeff392c5 100644 --- a/tests/DI/Definitions.LocatorDefinition.resolve.phpt +++ b/tests/DI/Definitions.LocatorDefinition.resolve.phpt @@ -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 () {