Skip to content

Commit e14684d

Browse files
committed
improved error messages
1 parent a1c22cb commit e14684d

21 files changed

+96
-58
lines changed

src/DI/Autowiring.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public function getByType(string $type, bool $throw = false): ?string
5050
$types = $this->highPriority;
5151
if (empty($types[$type])) {
5252
if ($throw) {
53-
throw new MissingServiceException("Service of type '$type' not found.");
53+
if (!class_exists($type) && !interface_exists($type)) {
54+
throw new MissingServiceException(sprintf("Service of type '%s' not found. Check the class name because it cannot be found.", $type));
55+
}
56+
throw new MissingServiceException(sprintf('Service of type %s not found. Did you add it to configuration file?', $type));
5457
}
5558
return null;
5659

src/DI/Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public function getByType(string $type, bool $throw = true)
234234

235235
} elseif ($throw) {
236236
if (!class_exists($type) && !interface_exists($type)) {
237-
throw new MissingServiceException("Service of type '$type' not found. Check class name because it cannot be found.");
237+
throw new MissingServiceException(sprintf("Service of type '%s' not found. Check the class name because it cannot be found.", $type));
238238
}
239239
foreach ($this->methods as $method => $foo) {
240240
$methodType = (new \ReflectionMethod(static::class, $method))->getReturnType()->getName();

src/DI/Definitions/AccessorDefinition.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ public function complete(Nette\DI\Resolver $resolver): void
9090
$returnType = Nette\DI\Helpers::getReturnType($method);
9191

9292
if (!$returnType) {
93-
throw new ServiceCreationException("Method $interface::get() has not return type hint or annotation @return.");
93+
throw new ServiceCreationException(sprintf('Method %s::get() has no return type or annotation @return.', $interface));
9494
} elseif (!class_exists($returnType) && !interface_exists($returnType)) {
95-
throw new ServiceCreationException("Check a type hint or annotation @return of the $interface::get() method, class '$returnType' cannot be found.");
95+
throw new ServiceCreationException(sprintf(
96+
"Class '%s' not found.\nCheck the return type or annotation @return of the %s::get() method.",
97+
$returnType,
98+
$interface
99+
));
96100
}
97101
$this->setReference($returnType);
98102
}

src/DI/Definitions/FactoryDefinition.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,13 @@ public function resolveType(Nette\DI\Resolver $resolver): void
182182
$method = new \ReflectionMethod($interface, self::METHOD_CREATE);
183183
$returnType = Nette\DI\Helpers::getReturnType($method);
184184
if (!$returnType) {
185-
throw new ServiceCreationException("Method $interface::create() has not return type hint or annotation @return.");
185+
throw new ServiceCreationException(sprintf('Method %s::create() has no return type or annotation @return.', $interface));
186186
} elseif (!class_exists($returnType) && !interface_exists($returnType)) {
187-
throw new ServiceCreationException("Check a type hint or annotation @return of the $interface::create() method, class '$returnType' cannot be found.");
187+
throw new ServiceCreationException(sprintf(
188+
"Class '%s' not found.\nCheck the return type or annotation @return of the %s::create() method.",
189+
$returnType,
190+
$interface
191+
));
188192
}
189193
$resultDef->setType($returnType);
190194
}
@@ -237,7 +241,12 @@ private function completeParameters(Nette\DI\Resolver $resolver): void
237241
if ($methodHint !== $ctorHint
238242
&& !is_a((string) reset($methodHint), (string) reset($ctorHint), true)
239243
) {
240-
throw new ServiceCreationException("Type hint for \${$param->name} in $interface::create() doesn't match type hint in $class constructor.");
244+
throw new ServiceCreationException(sprintf(
245+
"Type of \$%s in %s::create() doesn't match type in %s constructor.",
246+
$param->name,
247+
$interface,
248+
$class
249+
));
241250
}
242251
$this->resultDefinition->getFactory()->arguments[$ctorParam->getPosition()] = Nette\DI\ContainerBuilder::literal('$' . $ctorParam->name);
243252

src/DI/Extensions/InjectExtension.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,21 @@ private static function checkType($class, string $name, ?string $type, $containe
158158
{
159159
$propName = Reflection::toString(new \ReflectionProperty($class, $name));
160160
if (!$type) {
161-
throw new Nette\InvalidStateException("Property $propName has no type hint.");
161+
throw new Nette\InvalidStateException(sprintf('Property %s has no type.', $propName));
162+
162163
} elseif (!class_exists($type) && !interface_exists($type)) {
163-
throw new Nette\InvalidStateException("Class or interface '$type' used in type hint at $propName not found. Check type and 'use' statements.");
164+
throw new Nette\InvalidStateException(sprintf(
165+
"Class '%s' required by %s not found. Check the property type and 'use' statements.",
166+
$type,
167+
$propName
168+
));
169+
164170
} elseif ($container && !$container->getByType($type, false)) {
165-
throw new Nette\DI\MissingServiceException("Service of type $type used in type hint at $propName not found. Did you add it to configuration file?");
171+
throw new Nette\DI\MissingServiceException(sprintf(
172+
'Service of type %s required by %s not found. Did you add it to configuration file?',
173+
$type,
174+
$propName
175+
));
166176
}
167177
}
168178
}

src/DI/Resolver.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function resolveEntityType(Statement $statement): ?string
127127

128128
$type = Helpers::getReturnType($reflection);
129129
if ($type && !class_exists($type) && !interface_exists($type)) {
130-
throw new ServiceCreationException(sprintf("Class or interface '%s' not found. Is return type of %s() correct?", $type, Nette\Utils\Callback::toString($entity)));
130+
throw new ServiceCreationException(sprintf("Class or interface '%s' not found. Check the return type of %s() method.", $type, Nette\Utils\Callback::toString($entity)));
131131
}
132132
return $type;
133133

@@ -136,11 +136,12 @@ public function resolveEntityType(Statement $statement): ?string
136136

137137
} elseif (is_string($entity)) { // class
138138
if (!class_exists($entity)) {
139-
throw new ServiceCreationException(
139+
throw new ServiceCreationException(sprintf(
140140
interface_exists($entity)
141-
? "Interface $entity can not be used as 'factory', did you mean 'implement'?"
142-
: "Class $entity not found."
143-
);
141+
? "Interface %s can not be used as 'factory', did you mean 'implement'?"
142+
: "Class '%s' not found.",
143+
$entity
144+
));
144145
}
145146
return $entity;
146147
}
@@ -205,7 +206,7 @@ public function completeStatement(Statement $statement, bool $currentServiceAllo
205206

206207
case is_string($entity): // create class
207208
if (!class_exists($entity)) {
208-
throw new ServiceCreationException("Class $entity not found.");
209+
throw new ServiceCreationException(sprintf("Class '%s' not found.", $entity));
209210
} elseif ((new ReflectionClass($entity))->isAbstract()) {
210211
throw new ServiceCreationException("Class $entity is abstract.");
211212
} elseif (($rm = (new ReflectionClass($entity))->getConstructor()) !== null && !$rm->isPublic()) {
@@ -556,14 +557,22 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
556557
} catch (MissingServiceException $e) {
557558
$res = null;
558559
} catch (ServiceCreationException $e) {
559-
throw new ServiceCreationException("{$e->getMessage()} (needed by $desc)", 0, $e);
560+
throw new ServiceCreationException("{$e->getMessage()} (required by $desc)", 0, $e);
560561
}
561562
if ($res !== null || $parameter->allowsNull()) {
562563
return $res;
563564
} elseif (class_exists($type) || interface_exists($type)) {
564-
throw new ServiceCreationException("Service of type $type needed by $desc not found. Did you add it to configuration file?");
565+
throw new ServiceCreationException(sprintf(
566+
'Service of type %s required by %s not found. Did you add it to configuration file?',
567+
$type,
568+
$desc
569+
));
565570
} else {
566-
throw new ServiceCreationException("Class $type needed by $desc not found. Check type hint and 'use' statements.");
571+
throw new ServiceCreationException(sprintf(
572+
"Class '%s' required by %s not found. Check the parameter type and 'use' statements.",
573+
$type,
574+
$desc
575+
));
567576
}
568577

569578
} elseif (
@@ -587,8 +596,11 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
587596
: null;
588597

589598
} else {
590-
$tmp = count($types) > 1 ? 'union' : 'no class';
591-
throw new ServiceCreationException("Parameter $desc has $tmp type hint and no default value, so its value must be specified.");
599+
throw new ServiceCreationException(sprintf(
600+
'Parameter %s has %s, so its value must be specified.',
601+
$desc,
602+
count($types) > 1 ? 'union type and no default value' : 'no class type or default value'
603+
));
592604
}
593605
}
594606
}

tests/DI/Compiler.generatedFactory.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ Assert::exception(function () {
294294
->getResultDefinition()
295295
->setFactory('Bad1');
296296
$builder->complete();
297-
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad2): Type hint for \$bar in create() doesn't match type hint in Bad1 constructor.");
297+
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad2): Type of \$bar in create() doesn't match type in Bad1 constructor.");
298298

299299

300300

tests/DI/Compiler.generatedFactory.polymorphism.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Test: Nette\DI\Compiler: generated services factories from interfaces with class type hints in parameters.
4+
* Test: Nette\DI\Compiler: generated services factories from interfaces with class type in parameters.
55
*/
66

77
declare(strict_types=1);

tests/DI/Compiler.generatedFactory.scalarParameters.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Test: Nette\DI\Compiler: generated services factories from interfaces with scalar type hints in parameters.
4+
* Test: Nette\DI\Compiler: generated services factories from interfaces with scalar type in parameters.
55
*/
66

77
declare(strict_types=1);

tests/DI/Container.getByType.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Assert::null($container->getByType('unknown', false));
5555

5656
Assert::exception(function () use ($container) {
5757
$container->getByType('unknown');
58-
}, Nette\DI\MissingServiceException::class, "Service of type 'unknown' not found. Check class name because it cannot be found.");
58+
}, Nette\DI\MissingServiceException::class, "Service of type 'unknown' not found. Check the class name because it cannot be found.");
5959

6060
Assert::exception(function () use ($container) {
6161
$container->getByType('Exception');

0 commit comments

Comments
 (0)