diff --git a/src/Message/Handler/HandlerResolver.php b/src/Message/Handler/HandlerResolver.php index c1b4dec4..e6b51f10 100644 --- a/src/Message/Handler/HandlerResolver.php +++ b/src/Message/Handler/HandlerResolver.php @@ -56,7 +56,6 @@ public function __construct( * * @throws HandlerNotFoundException If no handler exists for the message type. * @throws InvalidHandlerConfigurationException If the handler definition is configured incorrectly. - * @throws ContainerExceptionInterface Error while retrieving the entry from container. */ public function resolve(string $messageType): HandlerInterface { @@ -72,7 +71,6 @@ public function resolve(string $messageType): HandlerInterface /** * @throws HandlerNotFoundException * @throws InvalidHandlerConfigurationException - * @throws ContainerExceptionInterface */ private function internalResolve(string $messageType): HandlerInterface { @@ -96,7 +94,6 @@ private function internalResolve(string $messageType): HandlerInterface /** * @throws HandlerNotFoundException * @throws InvalidHandlerConfigurationException - * @throws ContainerExceptionInterface */ private function getHandlerFromContainer(string $messageType, string $id): HandlerInterface { @@ -104,7 +101,11 @@ private function getHandlerFromContainer(string $messageType, string $id): Handl throw new HandlerNotFoundException($messageType); } - $handler = $this->container->get($id); + try { + $handler = $this->container->get($id); + } catch (ContainerExceptionInterface $exception) { + throw new InvalidHandlerConfigurationException($messageType, $exception->getMessage(), $exception); + } if ($handler instanceof HandlerInterface) { return $handler; @@ -126,13 +127,12 @@ private function getHandlerFromContainer(string $messageType, string $id): Handl /** * @throws InvalidHandlerConfigurationException - * @throws ContainerExceptionInterface */ private function createCallableHandler(string $messageType, mixed $definition): CallableHandler { try { $callable = $this->callableFactory->create($definition); - } catch (InvalidCallableConfigurationException $exception) { + } catch (InvalidCallableConfigurationException|ContainerExceptionInterface $exception) { throw new InvalidHandlerConfigurationException($messageType, $exception->getMessage(), $exception); } diff --git a/tests/Unit/Message/Handler/Resolver/HandlerResolverTest.php b/tests/Unit/Message/Handler/Resolver/HandlerResolverTest.php index ac0f13a5..e98c153b 100644 --- a/tests/Unit/Message/Handler/Resolver/HandlerResolverTest.php +++ b/tests/Unit/Message/Handler/Resolver/HandlerResolverTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use Yiisoft\Test\Support\Container\Exception\NotFoundException; use Yiisoft\Test\Support\Container\SimpleContainer; use Yiisoft\Queue\Message\Handler\HandlerNotFoundException; use Yiisoft\Queue\Message\Handler\HandlerResolver; @@ -181,6 +182,40 @@ public function handle(): void {} $resolver->resolve('invalid'); } + + public function testResolveThrowsWrappedContainerExceptionWhenGettingHandlerFromContainer(): void + { + $container = new SimpleContainer( + factory: static fn(string $id): mixed => throw new NotFoundException($id), + hasCallback: static fn(string $id): bool => true, + ); + $resolver = new HandlerResolver([], $container); + + $this->expectException(InvalidHandlerConfigurationException::class); + $this->expectExceptionMessage( + 'Queue handler for message type "simple" is configured incorrectly. No definition or class found for "simple".', + ); + $resolver->resolve('simple'); + } + + public function testResolveThrowsWrappedContainerExceptionWhenCreatingCallableHandler(): void + { + $container = new SimpleContainer( + factory: static fn(string $id): mixed => throw new NotFoundException($id), + hasCallback: static fn(string $id): bool => true, + ); + $resolver = new HandlerResolver( + ['simple' => ['NonExistentClassName', 'handle']], + $container, + ); + + $this->expectException(InvalidHandlerConfigurationException::class); + $this->expectExceptionMessage( + 'Queue handler for message type "simple" is configured incorrectly. No definition or class found for "NonExistentClassName".', + ); + + $resolver->resolve('simple'); + } } function namedFunctionHandler(MessageInterface $message): void