Files
BotPages/BotPages.Core/Pipeline/ErrorHandlingMiddleware.cs

24 lines
871 B
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace BotPages.Core
{
/// <summary>
/// Middleware обработки ошибок для надёжности.
/// </summary>
public sealed class ErrorHandlingMiddleware : IUpdateMiddleware
{
/// <summary>
/// Перехватывает исключения и отправляет сообщение об ошибке пользователю.
/// </summary>
public async Task InvokeAsync(UpdateContext ctx, Func<Task> next, CancellationToken ct)
{
try
{
await next();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex}");
await ctx.Client.SendTextAsync(ctx.Chat.Id, "Произошла ошибка. Попробуйте ещё раз. /start", null, ct);
}
}
}
}