Skip to content

[Messenger] catch all exceptions while deserializing a message #46996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
Expand Down