Skip to content

Commit

Permalink
ContainerBuilder: improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 30, 2019
1 parent 05a6aee commit 5b1d430
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
14 changes: 11 additions & 3 deletions src/DI/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,14 @@ public function complete()
$def->setSetup($setups);

} catch (\Exception $e) {
$message = "Service '$name' (type of {$def->getType()}): " . $e->getMessage();
$type = $def->getType();
if (!$type) {
$message = "Service '$name': " . $e->getMessage();
} elseif (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());
}
throw $e instanceof ServiceCreationException
? $e->setMessage($message)
: new ServiceCreationException($message, 0, $e);
Expand Down Expand Up @@ -702,8 +709,9 @@ public function completeStatement(Statement $statement)
} catch (ServiceCreationException $e) {
if ((is_string($entity) || is_array($entity)) && !strpos($e->getMessage(), ' (used in')) {
$desc = is_string($entity)
? $entity . '::__construct'
: (is_string($entity[0]) ? ($entity[0] . '::') : 'method ') . $entity[1];
? $entity . '::__construct()'
: (is_string($entity[0]) ? ($entity[0] . '::') : '')
. $entity[1] . (strpos($entity[1], '$') === false ? '()' : '');
$e->setMessage($e->getMessage() . " (used in $desc)");
}
throw $e;
Expand Down
4 changes: 2 additions & 2 deletions tests/DI/ContainerBuilder.autowiring.novalue.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 Foo::__construct() has no class type hint or default value, so its value must be specified.");
}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Foo): Parameter \$x in __construct() has no class type hint or default value, so its value must be specified.");


class Bar
Expand All @@ -36,4 +36,4 @@ 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 Bar::__construct() has no class type hint or default value, so its value must be specified.");
}, Nette\DI\ServiceCreationException::class, "Service 'foo' (type of Bar): Parameter \$x in __construct() has no class type hint or default value, so its value must be specified.");
28 changes: 14 additions & 14 deletions tests/DI/ContainerBuilder.factory.error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('one')->setFactory('Good', [new Statement('Unknown')]);
$builder->complete();
}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Unknown not found. (used in Good::__construct)");
}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Unknown not found. (used in __construct())");

// fail in argument
Assert::exception(function () {
$builder = new DI\ContainerBuilder;
$builder->addDefinition('one')->setFactory('Good', [new Statement('Bad8')]);
$builder->complete();
}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Bad8 has private constructor. (used in Good::__construct)");
}, Nette\InvalidStateException::class, "Service 'one' (type of Good): Class Bad8 has private constructor. (used in __construct())");


abstract class Bad9
Expand Down Expand Up @@ -227,7 +227,7 @@ services:
b: stdClass
bad: ConstructorParam
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (needed by \$x in ConstructorParam::__construct())");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (needed by \$x in __construct())");


// forced autowiring fail
Expand All @@ -238,7 +238,7 @@ services:
b: stdClass
bad: ConstructorParam(@\stdClass)
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (used in ConstructorParam::__construct)");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of ConstructorParam): Multiple services of type stdClass found: a, b (used in __construct())");


// autowiring fail in chain
Expand All @@ -249,7 +249,7 @@ services:
b: stdClass
bad: MethodParam()::foo()
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in MethodParam::foo())");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in foo())");


// forced autowiring fail in chain
Expand All @@ -260,7 +260,7 @@ services:
b: stdClass
bad: MethodParam()::foo(@\stdClass)
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (used in method foo)");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (used in foo())");


// autowiring fail in argument
Expand All @@ -271,7 +271,7 @@ services:
b: stdClass
bad: Good(ConstructorParam())
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in ConstructorParam::__construct()) (used in Good::__construct)");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in ConstructorParam::__construct()) (used in __construct())");


// forced autowiring fail in argument
Expand All @@ -282,7 +282,7 @@ services:
b: stdClass
bad: Good(ConstructorParam(@\stdClass))
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in ConstructorParam::__construct)");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in ConstructorParam::__construct())");


// autowiring fail in chain in argument
Expand All @@ -293,7 +293,7 @@ services:
b: stdClass
bad: Good(MethodParam()::foo())
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in MethodParam::foo()) (used in Good::__construct)");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (needed by \$x in MethodParam::foo()) (used in __construct())");


// forced autowiring fail in chain in argument
Expand All @@ -304,7 +304,7 @@ services:
b: stdClass
bad: Good(MethodParam()::foo(@\stdClass))
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in method foo)");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in foo())");


// forced autowiring fail in property passing
Expand Down Expand Up @@ -332,7 +332,7 @@ services:
setup:
- $a = MethodParam()::foo(@\stdClass)
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in method foo)");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in foo())");


// autowiring fail in method calling
Expand All @@ -346,7 +346,7 @@ services:
setup:
- foo
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in MethodParam::foo())");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of MethodParam): Multiple services of type stdClass found: a, b (needed by \$x in foo())");


// forced autowiring fail in method calling
Expand All @@ -360,7 +360,7 @@ services:
setup:
- bar(@\stdClass)
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in @bad::bar)");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in @bad::bar())");


// autowiring fail in rich method calling
Expand All @@ -374,4 +374,4 @@ services:
setup:
- bar(MethodParam()::foo(@\stdClass))
');
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in method foo)");
}, Nette\DI\ServiceCreationException::class, "Service 'bad' (type of Good): Multiple services of type stdClass found: a, b (used in foo())");

0 comments on commit 5b1d430

Please sign in to comment.