fix!: prevent exceptions from being muted

happens by not awaiting tasks
This commit is contained in:
ZavaruKitsu 2022-10-08 20:00:21 +03:00
parent f41fdf90ed
commit f9d25dfb83
9 changed files with 61 additions and 55 deletions

View File

@ -7,7 +7,7 @@ namespace TelegramBotBase.Base;
public static class Async
{
public delegate Task AsyncEventHandler<TEventArgs>(object sender, TEventArgs e) where TEventArgs : EventArgs;
public delegate Task AsyncEventHandler<in TEventArgs>(object sender, TEventArgs e) where TEventArgs : EventArgs;
public static IEnumerable<AsyncEventHandler<TEventArgs>> GetHandlers<TEventArgs>(
this AsyncEventHandler<TEventArgs> handler)

View File

@ -41,7 +41,6 @@ public class MessageClient
ApiKey = apiKey;
TelegramClient = new TelegramBotClient(apiKey, proxy);
Prepare();
}
@ -124,11 +123,9 @@ public class MessageClient
}
public Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
OnMessageLoop(new UpdateResult(update, null));
return Task.CompletedTask;
await OnMessageLoop(new UpdateResult(update, null));
}
public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
@ -161,6 +158,7 @@ public class MessageClient
/// This will set your bot commands to the given list.
/// </summary>
/// <param name="botcommands"></param>
/// <param name="languageCode"></param>
/// <returns></returns>
public async Task SetBotCommands(List<BotCommand> botcommands, BotCommandScope scope = null,
string languageCode = null)
@ -186,9 +184,9 @@ public class MessageClient
remove => Events.RemoveHandler(EvOnMessageLoop, value);
}
public void OnMessageLoop(UpdateResult update)
public async Task OnMessageLoop(UpdateResult update)
{
(Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
await (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
}
#endregion

View File

@ -107,11 +107,13 @@ public class BotBase
private async Task Client_MessageLoop(object sender, UpdateResult e)
{
try
{
var ds = Sessions.GetSession(e.DeviceId);
if (ds == null)
{
ds = Sessions.StartSession(e.DeviceId).GetAwaiter().GetResult();
ds = await Sessions.StartSession(e.DeviceId);
e.Device = ds;
ds.LastMessage = e.RawData.Message;
@ -135,6 +137,12 @@ public class BotBase
mr.IsFirstHandler = false;
} while (ds.FormSwitched && i < GetSetting(ESettings.NavigationMaximum, 10));
}
catch (Exception ex)
{
var ds = Sessions.GetSession(e.DeviceId);
OnException(new SystemExceptionEventArgs(e.Message.Text, e.DeviceId, ds, ex));
}
}
/// <summary>
@ -390,7 +398,7 @@ public class BotBase
}
/// <summary>
/// Will be called if no form handeled this call
/// Will be called if no form handled this call
/// </summary>
public event EventHandler<UnhandledCallEventArgs> UnhandledCall
{

View File

@ -41,24 +41,24 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
public BotBase Build()
{
var bb = new BotBase
var bot = new BotBase
{
ApiKey = _apiKey,
StartFormFactory = _factory,
Client = _client
};
bb.Sessions.Client = bb.Client;
bot.Sessions.Client = bot.Client;
bb.BotCommandScopes = BotCommandScopes;
bot.BotCommandScopes = BotCommandScopes;
bb.StateMachine = _statemachine;
bot.StateMachine = _statemachine;
bb.MessageLoopFactory = _messageLoopFactory;
bot.MessageLoopFactory = _messageLoopFactory;
bb.MessageLoopFactory.UnhandledCall += bb.MessageLoopFactory_UnhandledCall;
bot.MessageLoopFactory.UnhandledCall += bot.MessageLoopFactory_UnhandledCall;
return bb;
return bot;
}
public static IAPIKeySelectionStage Create()
@ -75,10 +75,10 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
}
public IBuildingStage QuickStart(string apiKey, Type StartForm)
public IBuildingStage QuickStart(string apiKey, Type startForm)
{
_apiKey = apiKey;
_factory = new DefaultStartFormFactory(StartForm);
_factory = new DefaultStartFormFactory(startForm);
DefaultMessageLoop();
@ -113,10 +113,10 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
return this;
}
public IBuildingStage QuickStart(string apiKey, IStartFormFactory StartFormFactory)
public IBuildingStage QuickStart(string apiKey, IStartFormFactory startFormFactory)
{
_apiKey = apiKey;
_factory = StartFormFactory;
_factory = startFormFactory;
DefaultMessageLoop();
@ -218,7 +218,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
TelegramClient =
{
Timeout = new TimeSpan(0, 1, 0)
}
},
};
return this;
}

View File

@ -219,7 +219,7 @@ public class AutoCleanForm : FormBase
var retryAfterSeconds = ex.InnerExceptions
.Where(e => e is ApiRequestException apiEx && apiEx.ErrorCode == 429)
.Max(e => ((ApiRequestException)e).Parameters.RetryAfter) ?? 0;
retryAfterTask = Task.Delay(retryAfterSeconds * 1000);
retryAfterTask = Task.Delay(retryAfterSeconds * 1000, cts.Token);
}
//deletedMessages.AsParallel().ForAll(i => Device.OnMessageDeleted(new MessageDeletedEventArgs(i)));

View File

@ -107,7 +107,7 @@ public class FormBaseMessageLoop : IMessageLoopFactory
}
/// <summary>
/// Will be called if no form handeled this call
/// Will be called if no form handled this call
/// </summary>
public event EventHandler<UnhandledCallEventArgs> UnhandledCall
{

View File

@ -100,7 +100,7 @@ public class FullMessageLoop : IMessageLoopFactory
}
/// <summary>
/// Will be called if no form handeled this call
/// Will be called if no form handled this call
/// </summary>
public event EventHandler<UnhandledCallEventArgs> UnhandledCall
{

View File

@ -31,7 +31,7 @@ public class MinimalMessageLoop : IMessageLoopFactory
}
/// <summary>
/// Will be called if no form handeled this call
/// Will be called if no form handled this call
/// </summary>
public event EventHandler<UnhandledCallEventArgs> UnhandledCall
{