Skip to content

Commit

Permalink
Merge branch '6.4' into 7.0
Browse files Browse the repository at this point in the history
* 6.4:
  [6.3] Remove unused test fixture
  [5.4] Remove unused test fixtures
  [Dotent] Add PHPDoc for `$overrideExistingVars`
  [SecurityBundle] Fix missing login-link element in xsd schema
  [Validator] Add missing Chinese translations #51934
  replace a not-existing virtual request stack with the real one
  [Messenger] add handler description as array key to `HandlerFailedException::getWrappedExceptions()`
  [Serializer] Fix using `DateIntervalNormalizer` with union types
  [Validator] fix: add missing translations for for Thai (th)
  fix #52273 [doctrine-messenger] DB table locks on messenger_messages with many failures
  [Serializer] Handle defaultContext for DateTimeNormalizer
  declare constructor argument as optional for backwards compatibility
  [CI] Add step to verify symfony/deprecation-contracts requirements
  • Loading branch information
fabpot committed Oct 26, 2023
2 parents e76c794 + 984c48b commit ed0d2aa
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 29 deletions.
7 changes: 4 additions & 3 deletions Exception/DelayedMessageHandlingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ class DelayedMessageHandlingException extends RuntimeException implements Wrappe
{
use WrappedExceptionsTrait;

private Envelope $envelope;
private array $exceptions;
private ?Envelope $envelope;

public function __construct(array $exceptions, Envelope $envelope)
public function __construct(array $exceptions, Envelope $envelope = null)
{
$this->envelope = $envelope;

Expand All @@ -45,7 +46,7 @@ public function __construct(array $exceptions, Envelope $envelope)
parent::__construct($message, 0, $exceptions[array_key_first($exceptions)]);
}

public function getEnvelope(): Envelope
public function getEnvelope(): ?Envelope
{
return $this->envelope;
}
Expand Down
2 changes: 1 addition & 1 deletion 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
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
21 changes: 0 additions & 21 deletions Tests/Stamp/StringErrorCodeException.php

This file was deleted.

2 changes: 1 addition & 1 deletion Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private function ack(): bool
$receiver->reject($envelope);
}

if ($e instanceof HandlerFailedException || $e instanceof DelayedMessageHandlingException) {
if ($e instanceof HandlerFailedException || ($e instanceof DelayedMessageHandlingException && null !== $e->getEnvelope())) {
$envelope = $e->getEnvelope();
}

Expand Down

0 comments on commit ed0d2aa

Please sign in to comment.