diff --git a/src/CommandBus/SymfonyCommandBus.php b/src/CommandBus/SymfonyCommandBus.php index 26d01fe0..958d0f7c 100644 --- a/src/CommandBus/SymfonyCommandBus.php +++ b/src/CommandBus/SymfonyCommandBus.php @@ -5,6 +5,7 @@ namespace Patchlevel\EventSourcingBundle\CommandBus; use Patchlevel\EventSourcing\CommandBus\CommandBus; +use Symfony\Component\Messenger\Exception\HandlerFailedException; use Symfony\Component\Messenger\MessageBusInterface; final class SymfonyCommandBus implements CommandBus @@ -16,6 +17,10 @@ public function __construct( public function dispatch(object $command): void { - $this->messageBus->dispatch($command); + try { + $this->messageBus->dispatch($command); + } catch (HandlerFailedException $e) { + throw $e->getWrappedExceptions(null, true)[0] ?? $e; + } } } diff --git a/tests/Unit/CommandBus/SymfonyCommandtBusTest.php b/tests/Unit/CommandBus/SymfonyCommandtBusTest.php new file mode 100644 index 00000000..ef5efc48 --- /dev/null +++ b/tests/Unit/CommandBus/SymfonyCommandtBusTest.php @@ -0,0 +1,88 @@ +prophesize(MessageBusInterface::class); + + $envelope = new Envelope($command); + + $symfony->dispatch($command)->willReturn($envelope)->shouldBeCalled(); + + $commandBus = new SymfonyCommandBus($symfony->reveal()); + $commandBus->dispatch($command); + } + + public function testException(): void + { + $command = new CreateProfile( + CustomId::fromString('1'), + ); + + $symfony = $this->prophesize(MessageBusInterface::class); + + $internalException = new class extends RuntimeException { + }; + + $envelope = new Envelope($command); + + $symfony + ->dispatch($command) + ->willThrow(new HandlerFailedException($envelope, [$internalException])) + ->shouldBeCalled(); + + $commandBus = new SymfonyCommandBus($symfony->reveal()); + + $this->expectException($internalException::class); + + $commandBus->dispatch($command); + } + + + public function testRecursiveException(): void + { + $command = new CreateProfile( + CustomId::fromString('1'), + ); + + $symfony = $this->prophesize(MessageBusInterface::class); + + $internalException = new class extends RuntimeException { + }; + + $envelope = new Envelope($command); + + $symfony + ->dispatch($command) + ->willThrow(new HandlerFailedException($envelope, [new HandlerFailedException($envelope, [$internalException])])) + ->shouldBeCalled(); + + $commandBus = new SymfonyCommandBus($symfony->reveal()); + + $this->expectException($internalException::class); + + $commandBus->dispatch($command); + } +} \ No newline at end of file