Adding exception for too long callback data.

- right now only in DEBUG compilation and only for the static operator
This commit is contained in:
Florian Zevedei
2024-02-03 01:39:12 +01:00
parent d572c353f6
commit 53886fa6bf
3 changed files with 40 additions and 2 deletions
+14 -2
View File
@@ -1,4 +1,6 @@
using Newtonsoft.Json;
using System.Text;
using TelegramBotBase.Exceptions;
namespace TelegramBotBase.Form;
@@ -30,7 +32,7 @@ public class CallbackData
/// Serializes data to json string
/// </summary>
/// <returns></returns>
public string Serialize()
public string Serialize(bool throwExceptionOnOverflow = false)
{
var s = "";
try
@@ -41,6 +43,16 @@ public class CallbackData
{
}
#if DEBUG
//Data is over 64 bytes
if(throwExceptionOnOverflow && Encoding.UTF8.GetByteCount(s) > Constants.Telegram.MaxCallBackDataBytes)
{
throw new CallbackDataTooLongException();
}
#endif
return s;
}
@@ -65,5 +77,5 @@ public class CallbackData
return null;
}
public static implicit operator string(CallbackData callbackData) => callbackData.Serialize();
public static implicit operator string(CallbackData callbackData) => callbackData.Serialize(true);
}