Skip to content

Commit dcc788d

Browse files
committed
[Messenger] add handler description as array key to HandlerFailedException::getWrappedExceptions()
1 parent fafbbfa commit dcc788d

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Add `WrappedExceptionsInterface` interface for exceptions that hold multiple individual exceptions
1313
* Deprecate `HandlerFailedException::getNestedExceptions()`, `HandlerFailedException::getNestedExceptionsOfClass()`
1414
and `DelayedMessageHandlingException::getExceptions()` which are replaced by a new `getWrappedExceptions()` method
15+
* Add handler description as array key to `HandlerFailedException::getWrappedExceptions()`
1516

1617
6.3
1718
---

src/Symfony/Component/Messenger/Exception/HandlerFailedException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class HandlerFailedException extends RuntimeException implements WrappedExceptio
2020
private Envelope $envelope;
2121

2222
/**
23-
* @param \Throwable[] $exceptions
23+
* @param array<string, \Throwable> $exceptions The name of the handler should be given as key
2424
*/
2525
public function __construct(Envelope $envelope, array $exceptions)
2626
{
@@ -55,7 +55,7 @@ public function getNestedExceptions(): array
5555
{
5656
trigger_deprecation('symfony/messenger', '6.4', 'The "%s()" method is deprecated, use "%s::getWrappedExceptions()" instead.', __METHOD__, self::class);
5757

58-
return $this->exceptions;
58+
return array_values($this->exceptions);
5959
}
6060

6161
/**

src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
6666
if ($batchHandler && $ackStamp = $envelope->last(AckStamp::class)) {
6767
$ack = new Acknowledger(get_debug_type($batchHandler), static function (\Throwable $e = null, $result = null) use ($envelope, $ackStamp, $handlerDescriptor) {
6868
if (null !== $e) {
69-
$e = new HandlerFailedException($envelope, [$e]);
69+
$e = new HandlerFailedException($envelope, [$handlerDescriptor->getName() => $e]);
7070
} else {
7171
$envelope = $envelope->with(HandledStamp::fromDescriptor($handlerDescriptor, $result));
7272
}
@@ -95,7 +95,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
9595
$envelope = $envelope->with($handledStamp);
9696
$this->logger?->info('Message {class} handled by {handler}', $context + ['handler' => $handledStamp->getHandlerName()]);
9797
} catch (\Throwable $e) {
98-
$exceptions[] = $e;
98+
$exceptions[$handlerDescriptor->getName()] = $e;
9999
}
100100
}
101101

@@ -107,7 +107,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
107107
$handler = $stamp->getHandlerDescriptor()->getBatchHandler();
108108
$handler->flush($flushStamp->force());
109109
} catch (\Throwable $e) {
110-
$exceptions[] = $e;
110+
$exceptions[$stamp->getHandlerDescriptor()->getName()] = $e;
111111
}
112112
}
113113
}

src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,31 @@ public function testItCallsTheHandlerAndNextMiddleware()
4747
$middleware->handle($envelope, $this->getStackMock());
4848
}
4949

50+
public function testItKeysTheHandlerFailedNestedExceptionsByHandlerDescription()
51+
{
52+
$message = new DummyMessage('Hey');
53+
$envelope = new Envelope($message);
54+
$handler = new FailingHandleMessageMiddlewareTestCallable();
55+
56+
$middleware = new HandleMessageMiddleware(new HandlersLocator([
57+
DummyMessage::class => [$handler],
58+
]));
59+
60+
try {
61+
$middleware->handle($envelope, $this->getStackMock(false));
62+
} catch (HandlerFailedException $e) {
63+
$key = (new HandlerDescriptor($handler))->getName();
64+
65+
$this->assertCount(1, $e->getWrappedExceptions());
66+
$this->assertArrayHasKey($key, $e->getWrappedExceptions());
67+
$this->assertSame('failed', $e->getWrappedExceptions()[$key]->getMessage());
68+
69+
return;
70+
}
71+
72+
$this->fail('Exception not thrown.');
73+
}
74+
5075
/**
5176
* @dataProvider itAddsHandledStampsProvider
5277
*/
@@ -335,6 +360,14 @@ public function __invoke()
335360
}
336361
}
337362

363+
class FailingHandleMessageMiddlewareTestCallable
364+
{
365+
public function __invoke()
366+
{
367+
throw new \Exception('failed');
368+
}
369+
}
370+
338371
class HandleMessageMiddlewareNamedArgumentTestCallable
339372
{
340373
public function __invoke(object $message, $namedArgument)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy