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

275c3af346 [Messenger] add handler description as array key to `HandlerFailedException::getWrappedExceptions()`
  • Loading branch information
nicolas-grekas committed Oct 25, 2023
2 parents a035441 + 43b7c3e commit 4343641
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Exception/HandlerFailedException.php
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions Middleware/HandleMessageMiddleware.php
Original file line number Diff line number Diff line change
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
30 changes: 30 additions & 0 deletions Tests/Middleware/HandleMessageMiddlewareTest.php
Original file line number Diff line number Diff line change
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 4343641

Please sign in to comment.