Merge pull request #51 from contributeless/feature/ability-to-log-exceptions-to-any-destination
Added the ReceiveError event to MessageClient to provide the ability to handle exceptions in client code
This commit is contained in:
commit
40199a1d81
14
TelegramBotBase/Base/ErrorResult.cs
Normal file
14
TelegramBotBase/Base/ErrorResult.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Base
|
||||||
|
{
|
||||||
|
public class ErrorResult : EventArgs
|
||||||
|
{
|
||||||
|
public ErrorResult(Exception exception)
|
||||||
|
{
|
||||||
|
Exception = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Exception Exception { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -18,6 +18,7 @@ namespace TelegramBotBase.Base;
|
|||||||
public class MessageClient
|
public class MessageClient
|
||||||
{
|
{
|
||||||
private static readonly object EvOnMessageLoop = new();
|
private static readonly object EvOnMessageLoop = new();
|
||||||
|
private static readonly object EvOnReceiveError = new();
|
||||||
|
|
||||||
private static object __evOnMessage = new();
|
private static object __evOnMessage = new();
|
||||||
|
|
||||||
@ -128,21 +129,11 @@ public class MessageClient
|
|||||||
await OnMessageLoop(new UpdateResult(update, null));
|
await OnMessageLoop(new UpdateResult(update, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
|
public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (exception is ApiRequestException exApi)
|
await OnReceiveError(new ErrorResult(exception));
|
||||||
{
|
|
||||||
Console.WriteLine($"Telegram API Error:\n[{exApi.ErrorCode}]\n{exApi.Message}");
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine(exception.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This will return the current list of bot commands.
|
/// This will return the current list of bot commands.
|
||||||
@ -186,7 +177,41 @@ public class MessageClient
|
|||||||
|
|
||||||
public async Task OnMessageLoop(UpdateResult update)
|
public async Task OnMessageLoop(UpdateResult update)
|
||||||
{
|
{
|
||||||
await (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
|
var eventHandlers = (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
|
||||||
|
|
||||||
|
if (eventHandlers != null)
|
||||||
|
{
|
||||||
|
await eventHandlers;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public event Async.AsyncEventHandler<ErrorResult> ReceiveError
|
||||||
|
{
|
||||||
|
add => Events.AddHandler(EvOnReceiveError, value);
|
||||||
|
remove => Events.RemoveHandler(EvOnReceiveError, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task OnReceiveError(ErrorResult update)
|
||||||
|
{
|
||||||
|
var eventHandlers = (Events[EvOnReceiveError] as Async.AsyncEventHandler<ErrorResult>)?.Invoke(this, update);
|
||||||
|
|
||||||
|
if (eventHandlers != null)
|
||||||
|
{
|
||||||
|
await eventHandlers;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Fallback when no event handler is used.
|
||||||
|
if (update.Exception is ApiRequestException exApi)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Telegram API Error:\n[{exApi.ErrorCode}]\n{exApi.Message}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine(update.Exception.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user