From c9ff84362c1d81a89d13cd01276c69bdf7487c2a Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Wed, 20 Jul 2022 15:25:37 +0200 Subject: [PATCH] [Messenger] catch all exceptions while deserializing message --- .../Serialization/SerializerTest.php | 31 +++++++++++++++++++ .../Transport/Serialization/Serializer.php | 5 ++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php index eff4d8614f109..a340b572cc430 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php @@ -21,6 +21,7 @@ use Symfony\Component\Messenger\Transport\Serialization\Serializer; use Symfony\Component\Serializer as SerializerComponent; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; +use Symfony\Component\Serializer\Serializer as SymfonySerializer; use Symfony\Component\Serializer\SerializerInterface as SerializerComponentInterface; class SerializerTest extends TestCase @@ -230,6 +231,36 @@ public function testDecodingStampFailedDeserialization() ], ]); } + + public function testDecodingFailedInCustomNormalizer() + { + $serializer = new Serializer( + new SymfonySerializer( + [ + new class() implements SerializerComponent\Normalizer\DenormalizerInterface { + public function denormalize($data, $type, string $format = null, array $context = []) + { + throw new \Exception('Error in custom denormalizer.'); + } + + public function supportsDenormalization($data, $type, string $format = null) + { + return true; + } + }, + ], + [new SerializerComponent\Encoder\JsonEncoder()] + ) + ); + + $this->expectException(MessageDecodingFailedException::class); + $this->expectExceptionMessage('Could not decode message: Error in custom denormalizer.'); + + $serializer->decode([ + 'body' => '{}', + 'headers' => ['type' => \stdClass::class], + ]); + } } class DummySymfonySerializerNonSendableStamp implements NonSendableStampInterface { diff --git a/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php b/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php index b5e93370027b6..ffb824ae9533c 100644 --- a/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php +++ b/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php @@ -19,7 +19,6 @@ use Symfony\Component\Messenger\Stamp\StampInterface; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Encoder\XmlEncoder; -use Symfony\Component\Serializer\Exception\ExceptionInterface; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer as SymfonySerializer; @@ -79,7 +78,7 @@ public function decode(array $encodedEnvelope): Envelope try { $message = $this->serializer->deserialize($encodedEnvelope['body'], $encodedEnvelope['headers']['type'], $this->format, $context); - } catch (ExceptionInterface $e) { + } catch (\Throwable $e) { throw new MessageDecodingFailedException('Could not decode message: '.$e->getMessage(), $e->getCode(), $e); } @@ -117,7 +116,7 @@ private function decodeStamps(array $encodedEnvelope): array try { $stamps[] = $this->serializer->deserialize($value, substr($name, \strlen(self::STAMP_HEADER_PREFIX)).'[]', $this->format, $this->context); - } catch (ExceptionInterface $e) { + } catch (\Throwable $e) { throw new MessageDecodingFailedException('Could not decode stamp: '.$e->getMessage(), $e->getCode(), $e); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy