Skip to content

[Messenger] Add UnwrapHandlerExceptionMiddleware #52949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Symfony\Component\Messenger\Middleware\RouterContextMiddleware;
use Symfony\Component\Messenger\Middleware\SendMessageMiddleware;
use Symfony\Component\Messenger\Middleware\TraceableMiddleware;
use Symfony\Component\Messenger\Middleware\UnwrapHandlerExceptionMiddleware;
use Symfony\Component\Messenger\Middleware\ValidationMiddleware;
use Symfony\Component\Messenger\Retry\MultiplierRetryStrategy;
use Symfony\Component\Messenger\RoutableMessageBus;
Expand Down Expand Up @@ -111,6 +112,8 @@
service('router'),
])

->set('messenger.middleware.unwrap_handler_exception', UnwrapHandlerExceptionMiddleware::class)

// Discovery
->set('messenger.receiver_locator', ServiceLocator::class)
->args([
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add option `redis_sentinel` as an alias for `sentinel_master`
* Add `--all` option to the `messenger:consume` command
* Make `#[AsMessageHandler]` final
* Add `UnwrapHandlerExceptionMiddleware`

7.0
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Middleware;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\LogicException;

class UnwrapHandlerExceptionMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
try {
return $stack->next()->handle($envelope, $stack);
} catch (HandlerFailedException $exception) {
$wrappedExceptions = $exception->getWrappedExceptions();

if (1 !== \count($wrappedExceptions)) {
throw new LogicException(sprintf('%s can only unwrap a single exception, but got %d.', __CLASS__, \count($wrappedExceptions)));
}

throw reset($wrappedExceptions);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests\Middleware;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Middleware\UnwrapHandlerExceptionMiddleware;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;

class UnwrapHandlerExceptionMiddlewareTest extends MiddlewareTestCase
{
public function testItThrowTheWrappedException()
{
$middleware = new UnwrapHandlerExceptionMiddleware();
$envelope = new Envelope(new \stdClass());
$wrappedException = new \RuntimeException('Wrapped exception.');
$exception = new HandlerFailedException($envelope, ['single handler' => $wrappedException]);

$this->expectException($wrappedException::class);
$this->expectExceptionMessage($wrappedException->getMessage());

$middleware->handle($envelope, $this->getThrowingStackMock($exception));
}

public function testItFailsWhenThereIsManyWrappedExceptions()
{
$middleware = new UnwrapHandlerExceptionMiddleware();
$envelope = new Envelope(new \stdClass());
$exception = new HandlerFailedException($envelope, [
'first handler' => new \RuntimeException('Wrapped exception.'),
'second handler' => new \RuntimeException('Wrapped exception.'),
]);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('"Symfony\Component\Messenger\Middleware\UnwrapHandlerExceptionMiddleware" can only unwrap a single exception, but got 2.');

$middleware->handle($envelope, $this->getThrowingStackMock($exception));
}
}