8000 [Messenger] Add `UnwrapHandlerExceptionMiddleware` · symfony/symfony@99e8e2b · GitHub
[go: up one dir, main page]

Skip to content

Commit 99e8e2b

Browse files
committed
[Messenger] Add UnwrapHandlerExceptionMiddleware
1 parent c4e97eb commit 99e8e2b

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Messenger\Middleware\RouterContextMiddleware;
3333
use Symfony\Component\Messenger\Middleware\SendMessageMiddleware;
3434
use Symfony\Component\Messenger\Middleware\TraceableMiddleware;
35+
use Symfony\Component\Messenger\Middleware\UnwrapHandlerExceptionMiddleware;
3536
use Symfony\Component\Messenger\Middleware\ValidationMiddleware;
3637
use Symfony\Component\Messenger\Retry\MultiplierRetryStrategy;
3738
use Symfony\Component\Messenger\RoutableMessageBus;
@@ -111,6 +112,8 @@
111112
service('router'),
112113
])
113114

115+
->set('messenger.middleware.unwrap_handler_exception', UnwrapHandlerExceptionMiddleware::class)
116+
114117
// Discovery
115118
->set('messenger.receiver_locator', ServiceLocator::class)
116119
->args([

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add option `redis_sentinel` as an alias for `sentinel_master`
88
* Add `--all` option to the `messenger:consume` command
9+
* Add `UnwrapHandlerExceptionMiddleware`
910

1011
7.0
1112
---
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Middleware;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
16+
use Symfony\Component\Messenger\Exception\LogicException;
17+
18+
class UnwrapHandlerExceptionMiddleware implements MiddlewareInterface
19+
{
20+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
21+
{
22+
try {
23+
return $stack->next()->handle($envelope, $stack);
24+
} catch (HandlerFailedException $exception) {
25+
$wrappedExceptions = $exception->getWrappedExceptions();
26+
27+
if (1 !== \count($wrappedExceptions)) {
28+
throw new LogicException(sprintf('"%s" can only unwrap a single exception, but got %d.', __CLASS__, \count($wrappedExceptions)));
29+
}
30+
31+
throw reset($wrappedExceptions);
32+
}
33+
}
34+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Middleware;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
16+
use Symfony\Component\Messenger\Exception\LogicException;
17+
use Symfony\Component\Messenger\Middleware\UnwrapHandlerExceptionMiddleware;
18+
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
19+
20+
class UnwrapHandlerExceptionMiddlewareTest extends MiddlewareTestCase
21+
{
22+
public function testItThrowTheWrappedException()
23+
{
24+
$middleware = new UnwrapHandlerExceptionMiddleware();
25+
$envelope = new Envelope(new \stdClass());
26+
$wrappedException = new \RuntimeException('Wrapped exception.');
27+
$exception = new HandlerFailedException($envelope, ['single handler' => $wrappedException]);
28+
29+
$this->expectException($wrappedException::class);
30+
$this->expectExceptionMessage($wrappedException->getMessage());
31+
32+
$middleware->handle($envelope, $this->getThrowingStackMock($exception));
33+
}
34+
35+
public function testItFailsWhenThereIsManyWrappedExceptions()
36+
{
37+
$middleware = new UnwrapHandlerExceptionMiddleware();
38+
$envelope = new Envelope(new \stdClass());
39+
$exception = new HandlerFailedException($envelope, [
40+
'first handler' => new \RuntimeException('Wrapped exception.'),
41+
'second handler' => new \RuntimeException('Wrapped exception.'),
42+
]);
43+
44+
$this->expectException(LogicException::class);
45+
$this->expectExceptionMessage('"Symfony\Component\Messenger\Middleware\UnwrapHandlerExceptionMiddleware" can only unwrap a single exception, but got 2.');
46+
47+
$middleware->handle($envelope, $this->getThrowingStackMock($exception));
48+
}
49+
}

0 commit comments

Comments
 (0)
0