8000 [Messenger] add handler description as array key to `HandlerFailedExc… · symfony/symfony@5e7bb97 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e7bb97

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

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

+1
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

+2-2
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/Exception/WrappedExceptionsInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
interface WrappedExceptionsInterface
2020
{
2121
/**
22-
* @return \Throwable[]
22+
* @return array<array-key, \Throwable>
2323
*/
2424
public function getWrappedExceptions(string $class = null, bool $recursive = false): array;
2525
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait WrappedExceptionsTrait
2121
private array $exceptions;
2222

2323
/**
24-
* @return \Throwable[]
24+
* @return array<array-key, \Throwable>
2525
*/
2626
public function getWrappedExceptions(string $class = null, bool $recursive = false): array
2727
{
@@ -32,7 +32,7 @@ public function getWrappedExceptions(string $class = null, bool $recursive = fal
3232
* @param class-string<\Throwable>|null $class
3333
* @param iterable<\Throwable> $exceptions
3434
*
35-
* @return \Throwable[]
35+
* @return array<array-key, \Throwable>
3636
*/
3737
private function getWrappedExceptionsRecursively(?string $class, bool $recursive, iterable $exceptions): array
3838
{

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

+3-3
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( 57AE 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

+33
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)
0