Skip to content

Commit 4efb16b

Browse files
bug #29226 [Messenger] Improved message when handler class does not exist (neeckeloo)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Messenger] Improved message when handler class does not exist | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? |no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | **Problem:** When defining a non existing messenger handler class in the `services.yml` config file, we encounter this confusing error message: ``` services: App\Handler\NonExistentHandler: tags: [messenger.message_handler] ``` ``` PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to Symfony\Component\Messenger\DependencyInjection\MessengerPass::guessHandledClasses() must be an instance of ReflectionClass, null given, called in /app/vendor/symfony/messenger/DependencyInjection/MessengerPass.php on line 93 in /app/vendor/symfony/messenger/DependencyInjection/MessengerPass.php:189 Stack trace: #0 /app/vendor/symfony/messenger/DependencyInjection/MessengerPass.php(93): Symfony\Component\Messenger\DependencyInjection\MessengerPass->guessHandledClasses(NULL, 'App\\Application...') #1 /app/vendor/symfony/messenger/DependencyInjection/MessengerPass.php(74): Symfony\Component\Messenger\DependencyInjection\MessengerPass->registerHandlers(Object(Symfony\Component\DependencyInjection\ContainerBuilder), Array) #2 /app/vendor/symfony/dependency-injection/Compiler/Compiler.php(95): Symfony\Component\Messenger\DependencyInjection\MessengerPass->process(Object(Symfony\Component\DependencyInjection\ContainerBuilder)) #3 / in /app/vendor/symfony/messenger/DependencyInjection/MessengerPass.php on line 189 ``` **Proposal:** We can throw a more relevant exception (RuntimeException) in this case to help the developer to have a better understanding of the issue. ```Invalid service "App\Handler\NonExistentHandler": class "App\Handler\NonExistentHandler" does not exist.``` Commits ------- 6ab9274 Improve error message when defining messenger handler class that does not exists
2 parents ed9193c + 6ab9274 commit 4efb16b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
7979
throw new RuntimeException(sprintf('Invalid handler service "%s": bus "%s" specified on the tag "%s" does not exist (known ones are: %s).', $serviceId, $tag['bus'], $this->handlerTag, implode(', ', $busIds)));
8080
}
8181

82-
$r = $container->getReflectionClass($container->getDefinition($serviceId)->getClass());
82+
$className = $container->getDefinition($serviceId)->getClass();
83+
$r = $container->getReflectionClass($className);
84+
85+
if (null === $r) {
86+
throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $serviceId, $className));
87+
}
8388

8489
if (isset($tag['handles'])) {
8590
$handles = isset($tag['method']) ? array($tag['handles'] => $tag['method']) : array($tag['handles']);

src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ public function testGetClassesAndMethodsAndPrioritiesFromTheSubscriber()
194194
$this->assertSame(PrioritizedHandler::class, $secondHandlerDefinition->getClass());
195195
}
196196

197+
/**
198+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
199+
* @expectedExceptionMessage Invalid service "NonExistentHandlerClass": class "NonExistentHandlerClass" does not exist.
200+
*/
201+
public function testThrowsExceptionIfTheHandlerClassDoesNotExist()
202+
{
203+
$container = $this->getContainerBuilder();
204+
$container->register('message_bus', MessageBusInterface::class)->addTag('messenger.bus');
205+
$container
206+
->register('NonExistentHandlerClass', 'NonExistentHandlerClass')
207+
->addTag('messenger.message_handler')
208+
;
209+
210+
(new MessengerPass())->process($container);
211+
}
212+
197213
/**
198214
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
199215
* @expectedExceptionMessage Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\HandlerMappingWithNonExistentMethod": method "Symfony\Component\Messenger\Tests\DependencyInjection\HandlerMappingWithNonExistentMethod::dummyMethod()" does not exist.

0 commit comments

Comments
 (0)