Skip to content

Commit

Permalink
improved style of error messages WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 28, 2021
1 parent 45e1252 commit f3608c4
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/DI/Extensions/InjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ private static function checkType(
if (!$type) {
throw new Nette\InvalidStateException("Property $propName has no type hint.");
} elseif (!class_exists($type) && !interface_exists($type)) {
throw new Nette\InvalidStateException("Class or interface '$type' used in type hint at $propName not found. Check type and 'use' statements.");
throw new Nette\InvalidStateException("Class or interface '$type' used in type hint at $propName not found.\nCheck type and 'use' statements.");
} elseif ($container && !$container->getByType($type, false)) {
throw new Nette\DI\MissingServiceException("Service of type $type used in type hint at $propName not found. Did you add it to configuration file?");
throw new Nette\DI\MissingServiceException("Service of type $type used in type hint at $propName not found.\nDid you add it to configuration file?");
}
}
}
18 changes: 9 additions & 9 deletions src/DI/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ public function completeStatement(Statement $statement, bool $currentServiceAllo
try {
$arguments = $this->completeArguments($arguments);
} catch (ServiceCreationException $e) {
if (!str_contains($e->getMessage(), ' (used in')) {
$e->setMessage($e->getMessage() . " (used in {$this->entityToString($entity)})");
if (!str_contains($e->getMessage(), "\nUsed in")) {
$e->setMessage($e->getMessage() . "\nUsed in {$this->entityToString($entity)}.");
}
throw $e;
}
Expand Down Expand Up @@ -404,18 +404,18 @@ public function addDependency(\ReflectionClass|\ReflectionFunctionAbstract|strin

private function completeException(\Exception $e, Definition $def): ServiceCreationException
{
if ($e instanceof ServiceCreationException && Strings::startsWith($e->getMessage(), "Service '")) {
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)" : '') . ': ';
$message = "(Service '$name'" . ($type ? " of type $type" : '') . ")\n";
} elseif ($type) {
$message = "Service of type $type: ";
$message = "(Service of type $type)\n";
} elseif ($def instanceof Definitions\ServiceDefinition && $def->getEntity()) {
$message = 'Service (' . $this->entityToString($def->getEntity()) . '): ';
$message = '(Service ' . $this->entityToString($def->getEntity()) . ")\n";
} else {
$message = '';
}
Expand Down Expand Up @@ -541,14 +541,14 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
} catch (MissingServiceException $e) {
$res = null;
} catch (ServiceCreationException $e) {
throw new ServiceCreationException("{$e->getMessage()} (needed by $desc)", 0, $e);
throw new ServiceCreationException($e->getMessage() . "\nNeeded by $desc.", 0, $e);
}
if ($res !== null || $parameter->allowsNull()) {
return $res;
} elseif (class_exists($type) || interface_exists($type)) {
throw new ServiceCreationException("Service of type $type needed by $desc not found. Did you add it to configuration file?");
throw new ServiceCreationException("Service of type $type needed by $desc not found.\nDid you add it to configuration file?");
} else {
throw new ServiceCreationException("Class $type needed by $desc not found. Check type hint and 'use' statements.");
throw new ServiceCreationException("Class $type needed by $desc not found.\nCheck type hint and 'use' statements.");
}

} elseif (
Expand Down
4 changes: 3 additions & 1 deletion tests/DI/Compiler.functions.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@ Assert::exception(function () {
services:
- Service(bool(123, 10))
');
}, Nette\InvalidStateException::class, 'Service of type Service: Function bool() expects at most 1 parameter, 2 given. (used in __construct())');
}, Nette\InvalidStateException::class, '(Service of type Service)
Function bool() expects at most 1 parameter, 2 given.
Used in __construct().');
9 changes: 6 additions & 3 deletions tests/DI/Compiler.generatedFactory.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ Assert::exception(function () {
->getResultDefinition()
->setFactory('Bad1');
$builder->complete();
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad2): Type hint for \$bar in create() doesn't match type hint in Bad1 constructor.");
}, Nette\InvalidStateException::class, "(Service 'one' of type Bad2)
Type hint for \$bar in create() doesn't match type hint in Bad1 constructor.");



Expand All @@ -317,7 +318,8 @@ Assert::exception(function () {
->getResultDefinition()
->setFactory('Bad3');
$builder->complete();
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad4): Unused parameter \$baz when implementing method create(), did you mean \$bar?");
}, Nette\InvalidStateException::class, "(Service 'one' of type Bad4)
Unused parameter \$baz when implementing method create(), did you mean \$bar?");



Expand All @@ -340,7 +342,8 @@ Assert::exception(function () {
->getResultDefinition()
->setFactory('Bad5');
$builder->complete();
}, Nette\InvalidStateException::class, "Service 'one' (type of Bad6): Unused parameter \$baz when implementing method create().");
}, Nette\InvalidStateException::class, "(Service 'one' of type Bad6)
Unused parameter \$baz when implementing method create().");



Expand Down
3 changes: 2 additions & 1 deletion tests/DI/Compiler.missingDefinition.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ Assert::throws(function () {
services:
foo:
');
}, Nette\InvalidStateException::class, "Service 'foo': Factory and type are missing in definition of service.");
}, Nette\InvalidStateException::class, "(Service 'foo')
Factory and type are missing in definition of service.");
6 changes: 4 additions & 2 deletions tests/DI/ContainerBuilder.autowiring.novalue.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('foo')->setType('Foo');
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Foo): Parameter \$x in __construct() has no class type hint and no default value, so its value must be specified.");
}, Nette\DI\ServiceCreationException::class, "(Service 'foo' of type Foo)
Parameter \$x in __construct() has no class type hint or default value, so its value must be specified.");


class Bar
Expand All @@ -38,7 +39,8 @@ Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('foo')->setType('Bar');
$container = createContainer($builder);
}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Bar): Parameter \$x in __construct() has no class type hint and no default value, so its value must be specified.");
}, Nette\DI\ServiceCreationException::class, "(Service 'foo' of type Bar)
Parameter \$x in __construct() has no class type hint or default value, so its value must be specified.");


class Bar2
Expand Down
6 changes: 4 additions & 2 deletions tests/DI/ContainerBuilder.error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ $builder->addDefinition('one')

Assert::exception(function () use ($builder) {
$builder->complete();
}, Nette\InvalidStateException::class, "Service 'one' (type of stdClass): Expected function, method or property name, '1234' given.");
}, Nette\InvalidStateException::class, "(Service 'one' of type stdClass)
Expected function, method or property name, '1234' given.");



Expand All @@ -43,4 +44,5 @@ $builder->addDefinition('one')

Assert::exception(function () use ($builder) {
$builder->complete();
}, Nette\InvalidStateException::class, "Service 'one' (type of stdClass): Missing argument for \$prop[].");
}, Nette\InvalidStateException::class, "(Service 'one' of type stdClass)
Missing argument for \$prop[].");
Loading

0 comments on commit f3608c4

Please sign in to comment.