Skip to content

Commit cf40c53

Browse files
authored
Merge pull request #1 from phphd/fix-envelope-stamps
fix: handled exception envelope stamps
2 parents c264029 + b77d90d commit cf40c53

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/ExceptionHandlerMiddleware.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1212
use Symfony\Component\Messenger\Middleware\StackInterface;
1313
use Symfony\Component\Messenger\Stamp\BusNameStamp;
14+
use Symfony\Component\Messenger\Stamp\HandledStamp;
1415
use Symfony\Component\Messenger\Stamp\HandlerArgumentsStamp;
1516
use Symfony\Component\String\ByteString;
1617
use Throwable;
@@ -33,11 +34,12 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
3334
return $stack->next()->handle($envelope, $stack);
3435
} catch (Exception $exception) {
3536
$exceptionBusName = $this->getExceptionBusName($busName);
37+
$handledExceptionEnvelope = $this->handleException($exceptionBusName, $exception, $message);
3638

37-
return $this->exceptionHandlerBus->dispatch(
38-
Envelope::wrap($exception, [new BusNameStamp($exceptionBusName)]),
39-
[new HandlerArgumentsStamp([$message])],
40-
);
39+
/** @var list<HandledStamp> $handledExceptionStamps */
40+
$handledExceptionStamps = $handledExceptionEnvelope->all(HandledStamp::class);
41+
42+
return $envelope->with(...$handledExceptionStamps);
4143
}
4244
}
4345

@@ -56,4 +58,12 @@ private function getExceptionBusName(ByteString $busName): string
5658
{
5759
return $busName->trimSuffix('.bus')->append('.exception.bus')->toString();
5860
}
61+
62+
private function handleException(string $exceptionBusName, Exception $exception, object $message): Envelope
63+
{
64+
return $this->exceptionHandlerBus->dispatch(
65+
Envelope::wrap($exception, [new BusNameStamp($exceptionBusName)]),
66+
[new HandlerArgumentsStamp([$message])],
67+
);
68+
}
5969
}

tests/Unit/ExceptionHandlerMiddlewareTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
use PhPhD\ExceptionHandler\ExceptionHandlerMiddleware;
99
use PHPUnit\Framework\MockObject\MockObject;
1010
use PHPUnit\Framework\TestCase;
11+
use RuntimeException;
1112
use stdClass;
1213
use Symfony\Component\Messenger\Envelope;
1314
use Symfony\Component\Messenger\MessageBus;
1415
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1516
use Symfony\Component\Messenger\Middleware\StackMiddleware;
17+
use Symfony\Component\Messenger\Stamp\BusNameStamp;
1618

1719
/**
1820
* @covers \PhPhD\ExceptionHandler\ExceptionHandlerMiddleware
@@ -45,4 +47,17 @@ public function testRequiresBusNameStamp(): void
4547

4648
$this->middleware->handle($envelope, $this->stack);
4749
}
50+
51+
public function testOriginalEnvelopeStampsArePreserved(): void
52+
{
53+
$this->nextMiddleware->method('handle')->willThrowException(new RuntimeException());
54+
55+
$envelope = Envelope::wrap(new stdClass(), [new BusNameStamp('command.bus')]);
56+
57+
$resultEnvelope = $this->middleware->handle($envelope, $this->stack);
58+
59+
$busNameStamp = $resultEnvelope->last(BusNameStamp::class);
60+
self::assertInstanceOf(BusNameStamp::class, $busNameStamp);
61+
self::assertSame('command.bus', $busNameStamp->getBusName());
62+
}
4863
}

0 commit comments

Comments
 (0)