diff --git a/generator/MTProtoGenerator.cs b/generator/MTProtoGenerator.cs index 5bd831a..e0025e3 100644 --- a/generator/MTProtoGenerator.cs +++ b/generator/MTProtoGenerator.cs @@ -208,7 +208,7 @@ public class MTProtoGenerator : IIncrementalGenerator foreach (var nullable in nullables) makeTL.AppendLine($"\t\t\t0x{nullable.Value:X8} => null,"); - makeTL.AppendLine("\t\t\tvar ctorNb => throw new Exception($\"Cannot find type for ctor #{ctorNb:x}\")"); + makeTL.AppendLine("\t\t\tvar ctorNb => throw new WTelegram.WTException($\"Cannot find type for ctor #{ctorNb:x}\")"); makeTL.AppendLine("\t\t};"); namespaces["TL"]["Layer"] = makeTL.ToString(); foreach (var namesp in namespaces) diff --git a/src/Helpers.cs b/src/Helpers.cs index 5562e9f..d483cb6 100644 --- a/src/Helpers.cs +++ b/src/Helpers.cs @@ -267,9 +267,7 @@ namespace WTelegram public class WTException : ApplicationException { - public readonly int ErrorCode; public WTException(string message) : base(message) { } - public WTException(string message, int code) : base(message) => ErrorCode = code; public WTException(string message, Exception innerException) : base(message, innerException) { } } } diff --git a/src/TL.cs b/src/TL.cs index ff07a6b..0045cef 100644 --- a/src/TL.cs +++ b/src/TL.cs @@ -32,9 +32,9 @@ namespace TL public readonly int Bit = bit; } - public sealed class RpcException(int code, string message, int x = -1) : WTelegram.WTException(message, code) + public sealed class RpcException(int code, string message, int x = -1) : WTelegram.WTException(message) { - public int Code => ErrorCode; + public readonly int Code = code; /// The value of X in the message, -1 if no variable X was found public readonly int X = x; public override string ToString() { var str = base.ToString(); return str.Insert(str.IndexOf(':') + 1, " " + Code); } @@ -278,8 +278,10 @@ namespace TL var dict = new Dictionary(count); for (int i = 0; i < count; i++) { - var value = (T)reader.ReadTLObject(); - dict[value.ID] = value is UserEmpty ? null : value; + var obj = reader.ReadTLObject(); + if (obj is T value) dict[value.ID] = value; + else if (obj is UserEmpty ue) dict[ue.id] = null; + else throw new InvalidCastException($"ReadTLDictionary got '{obj?.GetType().Name}' instead of '{typeof(T).Name}'"); } return dict; }