From 43b7c3e515be9f068bee10ffdcf77e20ed1fbcd6 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Wed, 9 Aug 2023 13:38:26 -0400 Subject: [PATCH] [Messenger] add handler description as array key to `HandlerFailedException::getWrappedExceptions()` --- Exception/HandlerFailedException.php | 4 +-- Middleware/HandleMessageMiddleware.php | 6 ++-- .../HandleMessageMiddlewareTest.php | 30 +++++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Exception/HandlerFailedException.php b/Exception/HandlerFailedException.php index 88ab12ac..f854238e 100644 --- a/Exception/HandlerFailedException.php +++ b/Exception/HandlerFailedException.php @@ -20,7 +20,7 @@ class HandlerFailedException extends RuntimeException implements WrappedExceptio private Envelope $envelope; /** - * @param \Throwable[] $exceptions + * @param \Throwable[] $exceptions The name of the handler should be given as key */ public function __construct(Envelope $envelope, array $exceptions) { @@ -55,7 +55,7 @@ public function getNestedExceptions(): array { trigger_deprecation('symfony/messenger', '6.4', 'The "%s()" method is deprecated, use "%s::getWrappedExceptions()" instead.', __METHOD__, self::class); - return $this->exceptions; + return array_values($this->exceptions); } /** diff --git a/Middleware/HandleMessageMiddleware.php b/Middleware/HandleMessageMiddleware.php index a8014a3e..c4e4a2d0 100644 --- a/Middleware/HandleMessageMiddleware.php +++ b/Middleware/HandleMessageMiddleware.php @@ -66,7 +66,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope if ($batchHandler && $ackStamp = $envelope->last(AckStamp::class)) { $ack = new Acknowledger(get_debug_type($batchHandler), static function (\Throwable $e = null, $result = null) use ($envelope, $ackStamp, $handlerDescriptor) { if (null !== $e) { - $e = new HandlerFailedException($envelope, [$e]); + $e = new HandlerFailedException($envelope, [$handlerDescriptor->getName() => $e]); } else { $envelope = $envelope->with(HandledStamp::fromDescriptor($handlerDescriptor, $result)); } @@ -95,7 +95,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope $envelope = $envelope->with($handledStamp); $this->logger?->info('Message {class} handled by {handler}', $context + ['handler' => $handledStamp->getHandlerName()]); } catch (\Throwable $e) { - $exceptions[] = $e; + $exceptions[$handlerDescriptor->getName()] = $e; } } @@ -107,7 +107,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope $handler = $stamp->getHandlerDescriptor()->getBatchHandler(); $handler->flush($flushStamp->force()); } catch (\Throwable $e) { - $exceptions[] = $e; + $exceptions[$stamp->getHandlerDescriptor()->getName()] = $e; } } } diff --git a/Tests/Middleware/HandleMessageMiddlewareTest.php b/Tests/Middleware/HandleMessageMiddlewareTest.php index d05cf736..13b0bb85 100644 --- a/Tests/Middleware/HandleMessageMiddlewareTest.php +++ b/Tests/Middleware/HandleMessageMiddlewareTest.php @@ -47,6 +47,36 @@ public function testItCallsTheHandlerAndNextMiddleware() $middleware->handle($envelope, $this->getStackMock()); } + public function testItKeysTheHandlerFailedNestedExceptionsByHandlerDescription() + { + $message = new DummyMessage('Hey'); + $envelope = new Envelope($message); + $handler = new class() { + public function __invoke() + { + throw new \Exception('failed'); + } + }; + + $middleware = new HandleMessageMiddleware(new HandlersLocator([ + DummyMessage::class => [$handler], + ])); + + try { + $middleware->handle($envelope, $this->getStackMock(false)); + } catch (HandlerFailedException $e) { + $key = (new HandlerDescriptor($handler))->getName(); + + $this->assertCount(1, $e->getWrappedExceptions()); + $this->assertArrayHasKey($key, $e->getWrappedExceptions()); + $this->assertSame('failed', $e->getWrappedExceptions()[$key]->getMessage()); + + return; + } + + $this->fail('Exception not thrown.'); + } + /** * @dataProvider itAddsHandledStampsProvider */