Skip to content

Commit f4d93e2

Browse files
[KttpKernel] Enhance exception message for services not found in dedicated service locators
1 parent 22a6a7e commit f4d93e2

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<argument type="service" id="controller_resolver" />
1919
<argument type="service" id="request_stack" />
2020
<argument type="service" id="argument_resolver" />
21+
<argument type="service" id="service_container" />
2122
<tag name="container.hot_path" />
2223
</service>
2324
<service id="Symfony\Component\HttpKernel\HttpKernelInterface" alias="http_kernel" />

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpKernel;
1313

14+
use Symfony\Component\DependencyInjection\Container;
15+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1416
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
1517
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
1618
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
@@ -42,13 +44,15 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
4244
protected $resolver;
4345
protected $requestStack;
4446
private $argumentResolver;
47+
private $container;
4548

46-
public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null, ArgumentResolverInterface $argumentResolver = null)
49+
public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null, ArgumentResolverInterface $argumentResolver = null, Container $container = null)
4750
{
4851
$this->dispatcher = $dispatcher;
4952
$this->resolver = $resolver;
5053
$this->requestStack = $requestStack ?: new RequestStack();
5154
$this->argumentResolver = $argumentResolver;
55+
$this->container = $container;
5256

5357
if (null === $this->argumentResolver) {
5458
@trigger_error(sprintf('As of 3.1 an %s is used to resolve arguments. In 4.0 the $argumentResolver becomes the %s if no other is provided instead of using the $resolver argument.', ArgumentResolverInterface::class, ArgumentResolver::class), E_USER_DEPRECATED);
@@ -67,6 +71,15 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
6771
try {
6872
return $this->handleRaw($request, $type);
6973
} catch (\Exception $e) {
74+
if ($this->container && $e instanceof ServiceNotFoundException) {
75+
$id = $e->getId();
76+
77+
if (!$e->getSourceId() && ($this->container->has($id) || isset($this->container->getRemovedIds()[$id]))) {
78+
$r = new \ReflectionProperty(\Exception::class, 'message');
79+
$r->setAccessible(true);
80+
$r->setValue($e, sprintf('Service "%s" exists, but not in the service locator of your own service. Did you forget to declare it as a dependency using e.g. "ServiceSubscriberInterface::getSubscribedServices()"?', $id));
81+
}
82+
}
7083
if ($e instanceof RequestExceptionInterface) {
7184
$e = new BadRequestHttpException($e->getMessage(), $e);
7285
}
@@ -150,7 +163,7 @@ private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
150163
$arguments = $event->getArguments();
151164

152165
// call controller
153-
$response = call_user_func_array($controller, $arguments);
166+
$response = \call_user_func_array($controller, $arguments);
154167

155168
// view
156169
if (!$response instanceof Response) {

0 commit comments

Comments
 (0)