Skip to content

Commit 3e34ef0

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

File tree

7 files changed

+45
-11
lines changed

7 files changed

+45
-11
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
@@ -21,7 +21,7 @@ class HandlerFailedException extends RuntimeException implements WrappedExceptio
2121
private Envelope $envelope;
2222

2323
/**
24-
* @param \Throwable[] $exceptions
24+
* @param array<string, \Throwable> $exceptions The name of the handler should be given as key
2525
*/
2626
public function __construct(Envelope $envelope, array $exceptions)
2727
{
@@ -50,7 +50,7 @@ public function getEnvelope(): Envelope
5050
/**
5151
* @deprecated since Symfony 6.4, use {@see self::getWrappedExceptions()} instead
5252
*
53-
* @return \Throwable[]
53+
* @return array<string, \Throwable> The name of the handler should be given as key
5454
*/
5555
public function getNestedExceptions(): array
5656
{

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

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
trait WrappedExceptionsTrait
2020
{
2121
/**
22-
* @return \Throwable[]
22+
* @return array<array-key, \Throwable>
2323
*/
2424
public function getWrappedExceptions(string $class = null, bool $recursive = false): array
2525
{
@@ -30,7 +30,7 @@ public function getWrappedExceptions(string $class = null, bool $recursive = fal
3030
* @param class-string<\Throwable>|null $class
3131
* @param iterable<\Throwable> $exceptions
3232
*
33-
* @return \Throwable[]
33+
* @return array<array-key, \Throwable>
3434
*/
3535
private function getWrappedExceptionsRecursively(?string $class, bool $recursive, iterable $exceptions): array
3636
{

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/Exception/HandlerFailedExceptionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function testThatWrappedExceptionsRecursive()
7575
$exception2 = new MyOwnException('second');
7676
$exception3 = new MyOwnException('third');
7777

78-
$handlerException = new HandlerFailedException($envelope, [$exception1, $exception2, new DelayedMessageHandlingException([$exception3])]);
78+
$handlerException = new HandlerFailedException($envelope, [$exception1, $exception2, new DelayedMessageHandlingException([$exception3], $envelope)]);
7979
$this->assertSame([$exception1, $exception2, $exception3], $handlerException->getWrappedExceptions(recursive: true));
8080
}
8181

@@ -86,7 +86,7 @@ public function testThatWrappedExceptionsRecursiveStringKeys()
8686
$exception2 = new MyOwnException('second');
8787
$exception3 = new MyOwnException('third');
8888

89-
$handlerException = new HandlerFailedException($envelope, ['first' => $exception1, 'second' => $exception2, new DelayedMessageHandlingException(['third' => $exception3])]);
89+
$handlerException = new HandlerFailedException($envelope, ['first' => $exception1, 'second' => $exception2, new DelayedMessageHandlingException(['third' => $exception3], $envelope)]);
9090
$this->assertSame(['first' => $exception1, 'second' => $exception2, 'third' => $exception3], $handlerException->getWrappedExceptions(recursive: true));
9191
}
9292

@@ -97,7 +97,7 @@ public function testThatWrappedExceptionsByClassRecursive()
9797
$exception2 = new MyOwnException('second');
9898
$exception3 = new MyOwnException('third');
9999

100-
$handlerException = new HandlerFailedException($envelope, [$exception1, $exception2, new DelayedMessageHandlingException([$exception3])]);
100+
$handlerException = new HandlerFailedException($envelope, [$exception1, $exception2, new DelayedMessageHandlingException([$exception3], $envelope)]);
101101
$this->assertSame([$exception2, $exception3], $handlerException->getWrappedExceptions(class: MyOwnException::class, recursive: true));
102102
}
103103
}

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