From 7753b0871be63e0d172f749123da187541e870f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Barray?= Date: Fri, 10 Feb 2023 15:41:37 +0100 Subject: [PATCH] fix: Unpack HandlerFailedException --- src/Service/SimpleBusDriver.php | 25 ++++--- tests/Unit/Service/SimpleBusDriverTest.php | 78 ++++++++++++++++++++++ 2 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 tests/Unit/Service/SimpleBusDriverTest.php diff --git a/src/Service/SimpleBusDriver.php b/src/Service/SimpleBusDriver.php index 53a8d54..b6e7590 100644 --- a/src/Service/SimpleBusDriver.php +++ b/src/Service/SimpleBusDriver.php @@ -4,6 +4,7 @@ use Psr\Log\LoggerInterface; use Symfony\Component\Messenger\Envelope; +use Symfony\Component\Messenger\Exception\HandlerFailedException; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp; use Symfony\Component\Messenger\Stamp\ReceivedStamp; @@ -20,14 +21,22 @@ public function __construct(LoggerInterface $logger) public function putEnvelopeOnBus(MessageBusInterface $bus, Envelope $envelope, string $transportName): void { - $envelope = $envelope->with(new ReceivedStamp($transportName), new ConsumedByWorkerStamp); - $bus->dispatch($envelope); + try { + $envelope = $envelope->with(new ReceivedStamp($transportName), new ConsumedByWorkerStamp); + $bus->dispatch($envelope); - $message = $envelope->getMessage(); - $this->logger->info('{class} was handled successfully.', [ - 'class' => get_class($message), - 'message' => $message, - 'transport' => $transportName, - ]); + $message = $envelope->getMessage(); + $this->logger->info('{class} was handled successfully.', [ + 'class' => get_class($message), + 'message' => $message, + 'transport' => $transportName, + ]); + } catch (HandlerFailedException $e) { + while ($e instanceof HandlerFailedException) { + $e = $e->getPrevious() ?? $e; + } + + throw $e; + } } } diff --git a/tests/Unit/Service/SimpleBusDriverTest.php b/tests/Unit/Service/SimpleBusDriverTest.php new file mode 100644 index 0000000..ee61fb9 --- /dev/null +++ b/tests/Unit/Service/SimpleBusDriverTest.php @@ -0,0 +1,78 @@ +messengerBus = $this->prophesize(MessageBusInterface::class); + } + + public function test_it_dispatch_message_on_specified_transport_ready_to_be_consumed() + { + $sut = new SimpleBusDriver(new NullLogger()); + $envelopeToBeConsumed = new Envelope(new stdClass()); + $this->messengerWillSucceedInConsumingOnTransport($envelopeToBeConsumed, 'my_transport'); + + $sut->putEnvelopeOnBus($this->messengerBus->reveal(), $envelopeToBeConsumed, 'my_transport'); + } + + public function test_it_unpack_handler_failed_exception() + { + $sut = new SimpleBusDriver(new NullLogger()); + $envelopeToBeConsumed = new Envelope(new stdClass()); + $this->messengerWillFailToConsumeOnTransport( + $envelopeToBeConsumed, + 'async', + new UnrecoverableMessageHandlingException('boum') + ); + $this->expectException(UnrecoverableMessageHandlingException::class); + $sut->putEnvelopeOnBus($this->messengerBus->reveal(), $envelopeToBeConsumed, 'async'); + } + + private function messengerWillSucceedInConsumingOnTransport(Envelope $envelope, string $transport) + { + $this->messengerBus + ->dispatch($envelope->with(new ReceivedStamp($transport), new ConsumedByWorkerStamp)) + ->willReturn( + $envelope->with(new HandledStamp('result', 'my.handler')) + ) + ->shouldBeCalled() + ; + } + + private function messengerWillFailToConsumeOnTransport(Envelope $envelope, string $transport, Throwable $failure) + { + $this->messengerBus + ->dispatch($envelope->with(new ReceivedStamp($transport), new ConsumedByWorkerStamp)) + ->willThrow( + new HandlerFailedException( + $envelope->with(new HandledStamp('result', 'my.handler')), + [ + $failure + ] + ) + ) + ->shouldBeCalled() + ; + } +}