ЛР 2: PostgreSQL, сессии, Redis и исследование

This commit is contained in:
Никита
2026-10-06 13:34:34 +03:00
parent 65003b6119
commit 6a8908ab99
111 changed files with 6686 additions and 38 deletions
@@ -0,0 +1,28 @@
using Messenger.Api.Services;
using System.Net.Sockets;
using Npgsql;
namespace Messenger.Api.Middleware;
public sealed class ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
{
public async Task InvokeAsync(HttpContext context)
{
try
{
await next(context);
}
catch (Exception exception) when (exception is NpgsqlException or SocketException or TimeoutException)
{
logger.LogError(exception, "Database request failed");
if (!context.Response.HasStarted)
await ApiSupport.ApiError(context, 503, "unavailable", "Сервис временно недоступен").ExecuteAsync(context);
}
catch (Exception exception)
{
logger.LogError(exception, "Request failed");
if (!context.Response.HasStarted)
await ApiSupport.ApiError(context, 500, "internal_error", "Внутренняя ошибка").ExecuteAsync(context);
}
}
}