Files
BotPages/BotPages.Core/Middleware/ErrorHandlingMiddleware.cs

39 lines
1.1 KiB
C#
Raw Permalink 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.
using BotPages.Core.Abstractions;
using BotPages.Core.Logging;
namespace BotPages.Core.Middleware;
/// <summary>
/// Middleware для глобальной ловли ошибок.
/// </summary>
public sealed class ErrorHandlingMiddleware : IPageMiddleware
{
private readonly ILogger _logger;
/// <inheritdoc/>
public ErrorHandlingMiddleware(ILogger logger)
{
_logger = logger;
}
/// <inheritdoc/>
public async Task InvokeAsync(PageContext ctx, Func<Task> next, CancellationToken ct)
{
try
{
await next();
}
catch (Exception ex)
{
_logger.Log(LogLevel.Critical, "Unhandled exception in middleware pipeline.", ex);
// Отправляем универсальный запрос с текстом об ошибке
await ctx.SendAsync(new SendRequest
{
ChatId = ctx.Update.Chat.Id,
Text = "Произошла ошибка при обработке запроса. Попробуйте ещё раз."
}, ct);
}
}
}