Skip to content

Commit 5d482d4

Browse files
author
janvt
authored
Merge pull request #48 from Bl00D4NGEL/fix-messenger-exception-handling
fix: logic to throw first wrapped exception in messenger busses
2 parents 165f0d7 + f88020f commit 5d482d4

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

src/Infrastructure/Messenger/CommandBus.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ public function dispatch(Command $command): mixed
2525
return $this->handle($command);
2626
} catch (HandlerFailedException $e) {
2727
$exceptions = $e->getWrappedExceptions();
28-
throw $exceptions[0];
28+
$first = array_shift($exceptions);
29+
if ($first) {
30+
throw $first;
31+
}
32+
33+
throw $e;
2934
}
3035
}
3136
}

src/Infrastructure/Messenger/QueryBus.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ public function dispatch(Query $query): mixed
2525
return $this->handle($query);
2626
} catch (HandlerFailedException $e) {
2727
$exceptions = $e->getWrappedExceptions();
28-
throw $exceptions[0];
28+
$first = array_shift($exceptions);
29+
if ($first) {
30+
throw $first;
31+
}
32+
33+
throw $e;
2934
}
3035
}
3136
}

tests/Unit/Infrastructure/Messenger/CommandBusTest.php

+29-6
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@
1111
use Symfony\Component\Messenger\MessageBus;
1212
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
1313

14-
/**
15-
* Test fixture for CommandBus.
16-
*/
1714
class TestCommand implements Command
1815
{
1916
}
2017

21-
/**
22-
* Test fixture for CommandBus.
23-
*/
2418
class TestCommandHandler
2519
{
2620
public function __invoke(TestCommand $command): mixed
@@ -29,6 +23,18 @@ public function __invoke(TestCommand $command): mixed
2923
}
3024
}
3125

26+
class ThrowingCommandHandler
27+
{
28+
public function __construct(private readonly \Exception $exceptionToThrow)
29+
{
30+
}
31+
32+
public function __invoke(TestCommand $command): mixed
33+
{
34+
throw $this->exceptionToThrow;
35+
}
36+
}
37+
3238
class CommandBusTest extends TestCase
3339
{
3440
public function testDispatch(): void
@@ -47,6 +53,23 @@ public function testDispatch(): void
4753
$this->assertSame(TestCommand::class, $result);
4854
}
4955

56+
public function testDispatchFailsAndRethrowsException(): void
57+
{
58+
// Given
59+
$expectedException = new \Exception('Not good enough');
60+
$bus = $this->createMessageBus(
61+
TestCommand::class,
62+
new ThrowingCommandHandler($expectedException)
63+
);
64+
$commandBus = new CommandBus($bus);
65+
66+
// Then
67+
$this->expectExceptionObject($expectedException);
68+
69+
// When
70+
$commandBus->dispatch(new TestCommand());
71+
}
72+
5073
private function createMessageBus(
5174
string $type,
5275
callable $handler

tests/Unit/Infrastructure/Messenger/QueryBusTest.php

+30-6
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@
55
namespace GeekCell\DddBundle\Tests\Unit\Infrastructure\Messenger;
66

77
use GeekCell\Ddd\Contracts\Application\Query;
8+
use GeekCell\DddBundle\Infrastructure\Messenger\CommandBus;
89
use GeekCell\DddBundle\Infrastructure\Messenger\QueryBus;
910
use PHPUnit\Framework\TestCase;
1011
use Symfony\Component\Messenger\Handler\HandlersLocator;
1112
use Symfony\Component\Messenger\MessageBus;
1213
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
1314

14-
/**
15-
* Test fixture for QueryBus.
16-
*/
1715
class TestQuery implements Query
1816
{
1917
}
2018

21-
/**
22-
* Test fixture for CommandBus.
23-
*/
19+
class ThrowingQueryHandler
20+
{
21+
public function __construct(private readonly \Exception $exceptionToThrow)
22+
{
23+
}
24+
25+
public function __invoke(TestQuery $query): mixed
26+
{
27+
throw $this->exceptionToThrow;
28+
}
29+
}
30+
2431
class TestQueryHandler
2532
{
2633
public function __invoke(TestQuery $query): mixed
@@ -47,6 +54,23 @@ public function testDispatch(): void
4754
$this->assertSame(TestQuery::class, $result);
4855
}
4956

57+
public function testDispatchFailsAndRethrowsException(): void
58+
{
59+
// Given
60+
$expectedException = new \Exception('Not good enough');
61+
$bus = $this->createMessageBus(
62+
TestQuery::class,
63+
new ThrowingQueryHandler($expectedException)
64+
);
65+
$commandBus = new QueryBus($bus);
66+
67+
// Then
68+
$this->expectExceptionObject($expectedException);
69+
70+
// When
71+
$commandBus->dispatch(new TestQuery());
72+
}
73+
5074
private function createMessageBus(
5175
string $type,
5276
callable $handler

0 commit comments

Comments
 (0)