Skip to content

Commit 8b3e4ce

Browse files
committed
Unwrap exception thrown in handler for Symfony Messenger 4.3
1 parent 8d0bd66 commit 8b3e4ce

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

features/json/input_output.feature

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature: JSON DTO input and output
77
Given I add "Accept" header equal to "application/json"
88
And I add "Content-Type" header equal to "application/json"
99

10-
Scenario: Messenger handler returning output object
10+
Scenario: Request a password reset
1111
And I send a "POST" request to "/users/password_reset_request" with body:
1212
"""
1313
{
@@ -23,3 +23,15 @@ Feature: JSON DTO input and output
2323
"emailSentAt": "2019-07-05T15:44:00+00:00"
2424
}
2525
"""
26+
27+
Scenario: Request a password reset for a non-existent user
28+
And I send a "POST" request to "/users/password_reset_request" with body:
29+
"""
30+
{
31+
"email": "[email protected]"
32+
}
33+
"""
34+
Then the response status code should be 404
35+
And the response should be in JSON
36+
And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8"
37+
And the JSON node "detail" should be equal to "User does not exist."

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
4545
use Symfony\Component\Finder\Finder;
4646
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
47-
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
47+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
4848
use Symfony\Component\Messenger\MessageBusInterface;
4949
use Symfony\Component\Validator\Validator\ValidatorInterface;
5050
use Symfony\Component\Yaml\Yaml;
@@ -526,7 +526,7 @@ private function registerMercureConfiguration(ContainerBuilder $container, array
526526
$loader->load('doctrine_orm_mercure_publisher.xml');
527527

528528
// BC for Symfony Messenger 4.2
529-
if (interface_exists(MessageBusInterface::class) && !class_exists(UnrecoverableMessageHandlingException::class)) {
529+
if (interface_exists(MessageBusInterface::class) && !class_exists(HandlerFailedException::class)) {
530530
$container->getDefinition('api_platform.doctrine.listener.mercure.publish')->replaceArgument(5, new Reference('message_bus', ContainerInterface::IGNORE_ON_INVALID_REFERENCE));
531531
}
532532
}
@@ -541,7 +541,7 @@ private function registerMessengerConfiguration(ContainerBuilder $container, arr
541541
$loader->load('messenger.xml');
542542

543543
// BC for Symfony Messenger 4.2
544-
if (interface_exists(MessageBusInterface::class) && !class_exists(UnrecoverableMessageHandlingException::class)) {
544+
if (interface_exists(MessageBusInterface::class) && !class_exists(HandlerFailedException::class)) {
545545
$container->setAlias('api_platform.message_bus', 'message_bus');
546546
}
547547
}

src/EventListener/ExceptionListener.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Psr\Log\LoggerInterface;
1818
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1919
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;
20+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
2021

2122
/**
2223
* Handles requests errors.
@@ -44,6 +45,18 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
4445
return;
4546
}
4647

48+
// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
49+
$exception = $event->getException();
50+
if ($exception instanceof HandlerFailedException) {
51+
/** @var \Throwable $previousException */
52+
$previousException = $exception->getPrevious();
53+
if (!$previousException instanceof \Exception) {
54+
throw $previousException;
55+
}
56+
57+
$event->setException($previousException);
58+
}
59+
4760
$this->exceptionListener->onKernelException($event);
4861
}
4962
}

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
9393
use Symfony\Component\DependencyInjection\Reference;
9494
use Symfony\Component\HttpFoundation\Response;
95-
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
95+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
9696
use Symfony\Component\Serializer\Exception\ExceptionInterface;
9797

9898
/**
@@ -494,7 +494,7 @@ public function testDisabledMessenger()
494494
{
495495
$containerBuilderProphecy = $this->getBaseContainerBuilderProphecy();
496496
$containerBuilderProphecy->setAlias('api_platform.message_bus', 'messenger.default_bus')->shouldNotBeCalled();
497-
if (!class_exists(UnrecoverableMessageHandlingException::class)) {
497+
if (!class_exists(HandlerFailedException::class)) {
498498
$containerBuilderProphecy->setAlias('api_platform.message_bus', 'message_bus')->shouldNotBeCalled();
499499
}
500500
$containerBuilderProphecy->setDefinition('api_platform.messenger.data_persister', Argument::type(Definition::class))->shouldNotBeCalled();
@@ -1175,7 +1175,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11751175
$containerBuilderProphecy->setAlias($alias, $service)->shouldBeCalled();
11761176
}
11771177

1178-
if (!class_exists(UnrecoverableMessageHandlingException::class)) {
1178+
if (!class_exists(HandlerFailedException::class)) {
11791179
$containerBuilderProphecy->setAlias('api_platform.message_bus', 'message_bus')->shouldBeCalled();
11801180
}
11811181

tests/Fixtures/TestBundle/MessageHandler/PasswordResetRequestHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515

1616
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\PasswordResetRequest;
1717
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Dto\PasswordResetRequestResult;
18+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1819
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
1920

2021
class PasswordResetRequestHandler implements MessageHandlerInterface
2122
{
2223
public function __invoke(PasswordResetRequest $passwordResetRequest): PasswordResetRequestResult
2324
{
25+
if ('[email protected]' === $passwordResetRequest->getEmail()) {
26+
throw new NotFoundHttpException('User does not exist.');
27+
}
28+
2429
return new PasswordResetRequestResult(new \DateTimeImmutable('2019-07-05T15:44:00Z'));
2530
}
2631
}

0 commit comments

Comments
 (0)