Skip to content

[HttpKernel] Enhance exception message for services not found in dedicated service locators #25367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<argument type="service" id="controller_resolver" />
<argument type="service" id="request_stack" />
<argument type="service" id="argument_resolver" />
<argument type="service" id="service_container" />
<tag name="container.hot_path" />
</service>
<service id="Symfony\Component\HttpKernel\HttpKernelInterface" alias="http_kernel" />
Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\HttpKernel;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
Expand Down Expand Up @@ -42,13 +44,15 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
protected $resolver;
protected $requestStack;
private $argumentResolver;
private $container;

public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null, ArgumentResolverInterface $argumentResolver = null)
public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null, ArgumentResolverInterface $argumentResolver = null, Container $container = null)
{
$this->dispatcher = $dispatcher;
$this->resolver = $resolver;
$this->requestStack = $requestStack ?: new RequestStack();
$this->argumentResolver = $argumentResolver;
$this->container = $container;

if (null === $this->argumentResolver) {
@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);
Expand All @@ -67,6 +71,15 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
try {
return $this->handleRaw($request, $type);
} catch (\Exception $e) {
if ($this->container && $e instanceof ServiceNotFoundException) {
$id = $e->getId();

if (!$e->getSourceId() && ($this->container->has($id) || isset($this->container->getRemovedIds()[$id]))) {
$r = new \ReflectionProperty(\Exception::class, 'message');
$r->setAccessible(true);
$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));
}
}
if ($e instanceof RequestExceptionInterface) {
$e = new BadRequestHttpException($e->getMessage(), $e);
}
Expand Down Expand Up @@ -150,7 +163,7 @@ private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
$arguments = $event->getArguments();

// call controller
$response = call_user_func_array($controller, $arguments);
$response = \call_user_func_array($controller, $arguments);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

improves perf, and DX, by removing one noisy frame in exception traces


// view
if (!$response instanceof Response) {
Expand Down