Skip to content

Commit

Permalink
bug #51331 [Messenger] add handler description as array key to `Handl…
Browse files Browse the repository at this point in the history
…erFailedException::getWrappedExceptions()` (kbond)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[Messenger] add handler description as array key to `HandlerFailedException::getWrappedExceptions()`

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a

Currently, when looking at `HandlerFailedException` to see what exceptions were thrown for a message, you can't see what handler caused the exception.

Commits
-------

275c3af [Messenger] add handler description as array key to `HandlerFailedException::getWrappedExceptions()`
  • Loading branch information
nicolas-grekas committed Oct 25, 2023
2 parents 68e8930 + 275c3af commit eb7d88d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}
}
Expand Down
Expand Up @@ -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
*/
Expand Down

0 comments on commit eb7d88d

Please sign in to comment.