From c1f47f0e8dead6bbacbc3a0638adeed3bb50f7cc Mon Sep 17 00:00:00 2001 From: Alex Bezdieniezhnykh Date: Wed, 16 Apr 2025 20:47:01 +0300 Subject: [PATCH] Revert "add correct business exception handling" This reverts commit b1693b2894027854524399082878ce30e72192d9. --- Azaion.Api/Program.cs | 3 --- Azaion.Common/Azaion.Common.csproj | 8 +------ Azaion.Common/BusinessException.cs | 2 +- Azaion.Common/BusinessExceptionHandler.cs | 27 ----------------------- Azaion.Common/Database/DbFactory.cs | 1 - Azaion.Common/Entities/User.cs | 2 +- Azaion.Services/Azaion.Services.csproj | 14 +++++++----- 7 files changed, 12 insertions(+), 45 deletions(-) delete mode 100644 Azaion.Common/BusinessExceptionHandler.cs diff --git a/Azaion.Api/Program.cs b/Azaion.Api/Program.cs index ee4aa99..9186e1e 100644 --- a/Azaion.Api/Program.cs +++ b/Azaion.Api/Program.cs @@ -1,5 +1,4 @@ using System.Text; -using Azaion.Common; using Azaion.Common.Configs; using Azaion.Common.Database; using Azaion.Common.Entities; @@ -108,7 +107,6 @@ builder.Services.AddScoped(); builder.Services.AddValidatorsFromAssemblyContaining(); -builder.Services.AddExceptionHandler(); var app = builder.Build(); if (app.Environment.IsDevelopment()) @@ -183,6 +181,5 @@ app.MapPut("/users/queue-offsets/{email}", .RequireAuthorization() .WithOpenApi(op => new OpenApiOperation(op) { Summary = "Updates user queue offsets" }); -app.UseExceptionHandler(_ => {}); app.Run(); diff --git a/Azaion.Common/Azaion.Common.csproj b/Azaion.Common/Azaion.Common.csproj index b7b6568..2ca3088 100644 --- a/Azaion.Common/Azaion.Common.csproj +++ b/Azaion.Common/Azaion.Common.csproj @@ -9,18 +9,12 @@ - + - - C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.12\Microsoft.AspNetCore.Diagnostics.dll - - - C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.12\Microsoft.AspNetCore.Http.Abstractions.dll - C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.Extensions.Options.dll diff --git a/Azaion.Common/BusinessException.cs b/Azaion.Common/BusinessException.cs index a149aa1..ae138e4 100644 --- a/Azaion.Common/BusinessException.cs +++ b/Azaion.Common/BusinessException.cs @@ -12,7 +12,7 @@ public class BusinessException(ExceptionEnum exEnum) : Exception(GetMessage(exEn ExceptionDescriptions = EnumExtensions.GetDescriptions(); } - public ExceptionEnum ExceptionEnum { get; set; } = exEnum; + private ExceptionEnum ExceptionEnum { get; set; } = exEnum; public static string GetMessage(ExceptionEnum exEnum) => ExceptionDescriptions.GetValueOrDefault(exEnum) ?? exEnum.ToString(); } diff --git a/Azaion.Common/BusinessExceptionHandler.cs b/Azaion.Common/BusinessExceptionHandler.cs deleted file mode 100644 index b4cb775..0000000 --- a/Azaion.Common/BusinessExceptionHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Microsoft.AspNetCore.Diagnostics; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; - -namespace Azaion.Common; - -public class BusinessExceptionHandler(ILogger logger) : IExceptionHandler -{ - public async ValueTask TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken) - { - if (exception is not BusinessException ex) - return false; - - logger.LogWarning(exception, ex.Message); - httpContext.Response.StatusCode = StatusCodes.Status409Conflict; - httpContext.Response.ContentType = "application/json"; - - var err = JsonConvert.SerializeObject(new - { - ErrorCode = ex.ExceptionEnum, - ex.Message - }); - await httpContext.Response.WriteAsync(err, cancellationToken).ConfigureAwait(false); - return true; - } -} \ No newline at end of file diff --git a/Azaion.Common/Database/DbFactory.cs b/Azaion.Common/Database/DbFactory.cs index 375eae5..20eccf7 100644 --- a/Azaion.Common/Database/DbFactory.cs +++ b/Azaion.Common/Database/DbFactory.cs @@ -3,7 +3,6 @@ using Azaion.Common.Configs; using LinqToDB; using Microsoft.Extensions.Options; - namespace Azaion.Common.Database; public interface IDbFactory diff --git a/Azaion.Common/Entities/User.cs b/Azaion.Common/Entities/User.cs index 9f6604b..836cd08 100644 --- a/Azaion.Common/Entities/User.cs +++ b/Azaion.Common/Entities/User.cs @@ -10,7 +10,7 @@ public class User public UserConfig? UserConfig { get; set; } = null!; - public static string GetCacheKey(string? email) => + public static string GetCacheKey(string email) => string.IsNullOrEmpty(email) ? "" : $"{nameof(User)}.{email}"; } diff --git a/Azaion.Services/Azaion.Services.csproj b/Azaion.Services/Azaion.Services.csproj index 92449da..1b9f5e8 100644 --- a/Azaion.Services/Azaion.Services.csproj +++ b/Azaion.Services/Azaion.Services.csproj @@ -1,5 +1,5 @@  - + net8.0 enable @@ -11,16 +11,20 @@ + + C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.AspNetCore.Http.Abstractions.dll + + C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.Extensions.Options.dll - - - - + + + +