Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/ExceptionHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\BusNameStamp;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Stamp\HandlerArgumentsStamp;
use Symfony\Component\String\ByteString;
use Throwable;
Expand All @@ -33,11 +34,12 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
return $stack->next()->handle($envelope, $stack);
} catch (Exception $exception) {
$exceptionBusName = $this->getExceptionBusName($busName);
$handledExceptionEnvelope = $this->handleException($exceptionBusName, $exception, $message);

return $this->exceptionHandlerBus->dispatch(
Envelope::wrap($exception, [new BusNameStamp($exceptionBusName)]),
[new HandlerArgumentsStamp([$message])],
);
/** @var list<HandledStamp> $handledExceptionStamps */
$handledExceptionStamps = $handledExceptionEnvelope->all(HandledStamp::class);

return $envelope->with(...$handledExceptionStamps);
}
}

Expand All @@ -56,4 +58,12 @@ private function getExceptionBusName(ByteString $busName): string
{
return $busName->trimSuffix('.bus')->append('.exception.bus')->toString();
}

private function handleException(string $exceptionBusName, Exception $exception, object $message): Envelope
{
return $this->exceptionHandlerBus->dispatch(
Envelope::wrap($exception, [new BusNameStamp($exceptionBusName)]),
[new HandlerArgumentsStamp([$message])],
);
}
}
15 changes: 15 additions & 0 deletions tests/Unit/ExceptionHandlerMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use PhPhD\ExceptionHandler\ExceptionHandlerMiddleware;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBus;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackMiddleware;
use Symfony\Component\Messenger\Stamp\BusNameStamp;

/**
* @covers \PhPhD\ExceptionHandler\ExceptionHandlerMiddleware
Expand Down Expand Up @@ -45,4 +47,17 @@ public function testRequiresBusNameStamp(): void

$this->middleware->handle($envelope, $this->stack);
}

public function testOriginalEnvelopeStampsArePreserved(): void
{
$this->nextMiddleware->method('handle')->willThrowException(new RuntimeException());

$envelope = Envelope::wrap(new stdClass(), [new BusNameStamp('command.bus')]);

$resultEnvelope = $this->middleware->handle($envelope, $this->stack);

$busNameStamp = $resultEnvelope->last(BusNameStamp::class);
self::assertInstanceOf(BusNameStamp::class, $busNameStamp);
self::assertSame('command.bus', $busNameStamp->getBusName());
}
}
Loading