From b1693b2894027854524399082878ce30e72192d9 Mon Sep 17 00:00:00 2001 From: Alex Bezdieniezhnykh Date: Wed, 16 Apr 2025 13:32:16 +0300 Subject: [PATCH] add correct business exception handling use instead of old nuget packages --- 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, 45 insertions(+), 12 deletions(-) create mode 100644 Azaion.Common/BusinessExceptionHandler.cs diff --git a/Azaion.Api/Program.cs b/Azaion.Api/Program.cs index 9186e1e..ee4aa99 100644 --- a/Azaion.Api/Program.cs +++ b/Azaion.Api/Program.cs @@ -1,4 +1,5 @@ using System.Text; +using Azaion.Common; using Azaion.Common.Configs; using Azaion.Common.Database; using Azaion.Common.Entities; @@ -107,6 +108,7 @@ builder.Services.AddScoped(); builder.Services.AddValidatorsFromAssemblyContaining(); +builder.Services.AddExceptionHandler(); var app = builder.Build(); if (app.Environment.IsDevelopment()) @@ -181,5 +183,6 @@ 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 2ca3088..b7b6568 100644 --- a/Azaion.Common/Azaion.Common.csproj +++ b/Azaion.Common/Azaion.Common.csproj @@ -9,12 +9,18 @@ - + + + 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 ae138e4..a149aa1 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(); } - private ExceptionEnum ExceptionEnum { get; set; } = exEnum; + public 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 new file mode 100644 index 0000000..b4cb775 --- /dev/null +++ b/Azaion.Common/BusinessExceptionHandler.cs @@ -0,0 +1,27 @@ +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 20eccf7..375eae5 100644 --- a/Azaion.Common/Database/DbFactory.cs +++ b/Azaion.Common/Database/DbFactory.cs @@ -3,6 +3,7 @@ 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 836cd08..9f6604b 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 1b9f5e8..92449da 100644 --- a/Azaion.Services/Azaion.Services.csproj +++ b/Azaion.Services/Azaion.Services.csproj @@ -1,5 +1,5 @@  - + net8.0 enable @@ -11,20 +11,16 @@ - - 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 - - - - + + + +