Skip to content

Commit 0d25785

Browse files
committed
improved style of error messages WIP
1 parent 43fdfa8 commit 0d25785

15 files changed

+124
-58
lines changed

src/DI/Extensions/InjectExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ private static function checkType(
153153
if (!$type) {
154154
throw new Nette\InvalidStateException("Property $propName has no type hint.");
155155
} elseif (!class_exists($type) && !interface_exists($type)) {
156-
throw new Nette\InvalidStateException("Class or interface '$type' used in type hint at $propName not found. Check type and 'use' statements.");
156+
throw new Nette\InvalidStateException("Class or interface '$type' used in type hint at $propName not found.\nCheck type and 'use' statements.");
157157
} elseif ($container && !$container->getByType($type, false)) {
158-
throw new Nette\DI\MissingServiceException("Service of type $type used in type hint at $propName not found. Did you add it to configuration file?");
158+
throw new Nette\DI\MissingServiceException("Service of type $type used in type hint at $propName not found.\nDid you add it to configuration file?");
159159
}
160160
}
161161
}

src/DI/Resolver.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ public function completeStatement(Statement $statement, bool $currentServiceAllo
269269
try {
270270
$arguments = $this->completeArguments($arguments);
271271
} catch (ServiceCreationException $e) {
272-
if (!strpos($e->getMessage(), ' (used in')) {
273-
$e->setMessage($e->getMessage() . " (used in {$this->entityToString($entity)})");
272+
if (!strpos($e->getMessage(), "\nUsed in")) {
273+
$e->setMessage($e->getMessage() . "\nUsed in {$this->entityToString($entity)}.");
274274
}
275275
throw $e;
276276
}
@@ -404,18 +404,18 @@ public function addDependency(\ReflectionClass|\ReflectionFunctionAbstract|strin
404404

405405
private function completeException(\Exception $e, Definition $def): ServiceCreationException
406406
{
407-
if ($e instanceof ServiceCreationException && Strings::startsWith($e->getMessage(), "Service '")) {
407+
if ($e instanceof ServiceCreationException && Strings::startsWith($e->getMessage(), "(Service '")) {
408408
return $e;
409409
}
410410

411411
$name = $def->getName();
412412
$type = $def->getType();
413413
if ($name && !ctype_digit($name)) {
414-
$message = "Service '$name'" . ($type ? " (type of $type)" : '') . ': ';
414+
$message = "(Service '$name'" . ($type ? " of type $type" : '') . ")\n";
415415
} elseif ($type) {
416-
$message = "Service of type $type: ";
416+
$message = "(Service of type $type)\n";
417417
} elseif ($def instanceof Definitions\ServiceDefinition && $def->getEntity()) {
418-
$message = 'Service (' . $this->entityToString($def->getEntity()) . '): ';
418+
$message = '(Service ' . $this->entityToString($def->getEntity()) . ")\n";
419419
} else {
420420
$message = '';
421421
}
@@ -541,14 +541,14 @@ private static function autowireArgument(\ReflectionParameter $parameter, callab
541541
} catch (MissingServiceException $e) {
542542
$res = null;
543543
} catch (ServiceCreationException $e) {
544-
throw new ServiceCreationException("{$e->getMessage()} (needed by $desc)", 0, $e);
544+
throw new ServiceCreationException($e->getMessage() . "\nNeeded by $desc.", 0, $e);
545545
}
546546
if ($res !== null || $parameter->allowsNull()) {
547547
return $res;
548548
} elseif (class_exists($type) || interface_exists($type)) {
549-
throw new ServiceCreationException("Service of type $type needed by $desc not found. Did you add it to configuration file?");
549+
throw new ServiceCreationException("Service of type $type needed by $desc not found.\nDid you add it to configuration file?");
550550
} else {
551-
throw new ServiceCreationException("Class $type needed by $desc not found. Check type hint and 'use' statements.");
551+
throw new ServiceCreationException("Class $type needed by $desc not found.\nCheck type hint and 'use' statements.");
552552
}
553553

554554
} elseif (

tests/DI/Compiler.functions.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,6 @@ Assert::exception(function () {
8585
services:
8686
- Service(bool(123, 10))
8787
');
88-
}, Nette\InvalidStateException::class, 'Service of type Service: Function bool() expects at most 1 parameter, 2 given. (used in __construct())');
88+
}, Nette\InvalidStateException::class, '(Service of type Service)
89+
Function bool() expects at most 1 parameter, 2 given.
90+
Used in __construct().');

tests/DI/Compiler.generatedFactory.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ 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' of type Bad2)
298+
Type hint for \$bar in create() doesn't match type hint in Bad1 constructor.");
298299

299300

300301

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

322324

323325

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

345348

346349

tests/DI/Compiler.missingDefinition.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ Assert::throws(function () {
2222
services:
2323
foo:
2424
');
25-
}, Nette\InvalidStateException::class, "Service 'foo': Factory and type are missing in definition of service.");
25+
}, Nette\InvalidStateException::class, "(Service 'foo')
26+
Factory and type are missing in definition of service.");

tests/DI/ContainerBuilder.autowiring.novalue.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Assert::exception(function () {
2424
$builder = new DI\ContainerBuilder;
2525
$builder->addDefinition('foo')->setType('Foo');
2626
$container = createContainer($builder);
27-
}, 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.");
27+
}, Nette\DI\ServiceCreationException::class, "(Service 'foo' of type Foo)
28+
Parameter \$x in __construct() has no class type hint or default value, so its value must be specified.");
2829

2930

3031
class Bar
@@ -38,7 +39,8 @@ Assert::exception(function () {
3839
$builder = new DI\ContainerBuilder;
3940
$builder->addDefinition('foo')->setType('Bar');
4041
$container = createContainer($builder);
41-
}, 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.");
42+
}, Nette\DI\ServiceCreationException::class, "(Service 'foo' of type Bar)
43+
Parameter \$x in __construct() has no class type hint or default value, so its value must be specified.");
4244

4345

4446
class Bar2

tests/DI/ContainerBuilder.error.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ $builder->addDefinition('one')
2020

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

2526

2627

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

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

0 commit comments

Comments
 (0)