From d0619ba23df993686089d65bb1dcea011e9ce938 Mon Sep 17 00:00:00 2001 From: Rhodri Pugh Date: Wed, 20 Mar 2024 15:15:13 +0000 Subject: [PATCH] [Messenger] swallow exception rolling back transaction if there is an exception rolling back the transaction then the original exception will be hidden. --- .../Messenger/DoctrineTransactionMiddleware.php | 5 ++++- .../DoctrineTransactionMiddlewareTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php b/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php index e4831557f01db..a743a34ce6b4a 100644 --- a/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php +++ b/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php @@ -34,7 +34,10 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel return $envelope; } catch (\Throwable $exception) { - $entityManager->getConnection()->rollBack(); + try { + $entityManager->getConnection()->rollBack(); + } catch (\Throwable $e) { + } if ($exception instanceof HandlerFailedException) { // Remove all HandledStamp from the envelope so the retry will execute all handlers again. diff --git a/src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineTransactionMiddlewareTest.php b/src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineTransactionMiddlewareTest.php index f0eb0b22efcf4..cc2bc31624af8 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineTransactionMiddlewareTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineTransactionMiddlewareTest.php @@ -69,6 +69,22 @@ public function testTransactionIsRolledBackOnException() $this->middleware->handle(new Envelope(new \stdClass()), $this->getThrowingStackMock()); } + public function testExceptionRollingBackTransactionSwallowed() + { + $this->connection->expects($this->once()) + ->method('beginTransaction') + ; + $this->connection->expects($this->once()) + ->method('rollBack') + ->will($this->throwException(new \Exception('Could not roll back transaction.'))) + ; + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Thrown from next middleware.'); + + $this->middleware->handle(new Envelope(new \stdClass()), $this->getThrowingStackMock()); + } + public function testInvalidEntityManagerThrowsException() { $managerRegistry = $this->createMock(ManagerRegistry::class);