< Summary

Information
Class: TeleFlow.Telegram.Internal.TelegramUpdateDecoder
Assembly: TeleFlow.Telegram.Client
File(s): /_/src/TeleFlow.Telegram.Client/Internal/TelegramUpdateDecoder.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 49
Line coverage: 100%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Decode(...)50%44100%

File(s)

/_/src/TeleFlow.Telegram.Client/Internal/TelegramUpdateDecoder.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using System.Text;
 3using System.Text.Json;
 4using TeleFlow.Telegram.Schema.Types;
 5
 6namespace TeleFlow.Telegram.Internal;
 7
 8/// <summary>
 9/// Decodes one known Telegram update envelope and captures raw payload evidence only when schema mapping fails.
 10/// </summary>
 11internal static class TelegramUpdateDecoder
 12{
 13    public static TelegramUpdateDecodeResult Decode(
 14        JsonElement payload,
 15        long updateId,
 16        JsonSerializerOptions serializerOptions,
 17        string? methodName = null)
 18    {
 3019        ArgumentNullException.ThrowIfNull(serializerOptions);
 20
 21        try
 22        {
 3023            var update = payload.Deserialize<Update>(serializerOptions)
 3024                ?? throw new JsonException("Telegram update payload deserialized to null.");
 25
 2226            return TelegramUpdateDecodeResult.Success(update);
 27        }
 828        catch (Exception exception) when (exception is JsonException or NotSupportedException)
 29        {
 830            var rawPayloadJson = payload.GetRawText();
 831            var payloadSha256 = Convert.ToHexStringLower(
 832                SHA256.HashData(Encoding.UTF8.GetBytes(rawPayloadJson)));
 833            var jsonPath = exception is JsonException jsonException ? jsonException.Path : null;
 834            var decodeException = new TelegramUpdateDecodeException(
 835                $"Failed to deserialize Telegram update '{updateId}'.",
 836                updateId,
 837                payloadSha256,
 838                jsonPath,
 839                exception,
 840                methodName);
 41
 842            return TelegramUpdateDecodeResult.Failure(
 843                updateId,
 844                rawPayloadJson,
 845                payloadSha256,
 846                decodeException);
 47        }
 3048    }
 49}