mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 23:36:34 +00:00
add correct business exception handling
use <FrameworkReference Include="Microsoft.AspNetCore.App" /> instead of old nuget packages
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Azaion.Common;
|
||||||
using Azaion.Common.Configs;
|
using Azaion.Common.Configs;
|
||||||
using Azaion.Common.Database;
|
using Azaion.Common.Database;
|
||||||
using Azaion.Common.Entities;
|
using Azaion.Common.Entities;
|
||||||
@@ -107,6 +108,7 @@ builder.Services.AddScoped<ICache, MemoryCache>();
|
|||||||
|
|
||||||
builder.Services.AddValidatorsFromAssemblyContaining<RegisterUserValidator>();
|
builder.Services.AddValidatorsFromAssemblyContaining<RegisterUserValidator>();
|
||||||
|
|
||||||
|
builder.Services.AddExceptionHandler<BusinessExceptionHandler>();
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
@@ -181,5 +183,6 @@ app.MapPut("/users/queue-offsets/{email}",
|
|||||||
.RequireAuthorization()
|
.RequireAuthorization()
|
||||||
.WithOpenApi(op => new OpenApiOperation(op) { Summary = "Updates user queue offsets" });
|
.WithOpenApi(op => new OpenApiOperation(op) { Summary = "Updates user queue offsets" });
|
||||||
|
|
||||||
|
app.UseExceptionHandler(_ => {});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
@@ -9,12 +9,18 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentValidation" Version="11.10.0" />
|
<PackageReference Include="FluentValidation" Version="11.10.0" />
|
||||||
<PackageReference Include="linq2db" Version="5.4.1" />
|
<PackageReference Include="linq2db" Version="5.4.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
|
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.4" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||||
<PackageReference Include="Npgsql" Version="8.0.5" />
|
<PackageReference Include="Npgsql" Version="8.0.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Microsoft.AspNetCore.Diagnostics">
|
||||||
|
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.12\Microsoft.AspNetCore.Diagnostics.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Microsoft.AspNetCore.Http.Abstractions">
|
||||||
|
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.12\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="Microsoft.Extensions.Options">
|
<Reference Include="Microsoft.Extensions.Options">
|
||||||
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.Extensions.Options.dll</HintPath>
|
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.Extensions.Options.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class BusinessException(ExceptionEnum exEnum) : Exception(GetMessage(exEn
|
|||||||
ExceptionDescriptions = EnumExtensions.GetDescriptions<ExceptionEnum>();
|
ExceptionDescriptions = EnumExtensions.GetDescriptions<ExceptionEnum>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExceptionEnum ExceptionEnum { get; set; } = exEnum;
|
public ExceptionEnum ExceptionEnum { get; set; } = exEnum;
|
||||||
|
|
||||||
public static string GetMessage(ExceptionEnum exEnum) => ExceptionDescriptions.GetValueOrDefault(exEnum) ?? exEnum.ToString();
|
public static string GetMessage(ExceptionEnum exEnum) => ExceptionDescriptions.GetValueOrDefault(exEnum) ?? exEnum.ToString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<BusinessExceptionHandler> logger) : IExceptionHandler
|
||||||
|
{
|
||||||
|
public async ValueTask<bool> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ using Azaion.Common.Configs;
|
|||||||
using LinqToDB;
|
using LinqToDB;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
|
||||||
namespace Azaion.Common.Database;
|
namespace Azaion.Common.Database;
|
||||||
|
|
||||||
public interface IDbFactory
|
public interface IDbFactory
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ public class User
|
|||||||
|
|
||||||
public UserConfig? UserConfig { get; set; } = null!;
|
public UserConfig? UserConfig { get; set; } = null!;
|
||||||
|
|
||||||
public static string GetCacheKey(string email) =>
|
public static string GetCacheKey(string? email) =>
|
||||||
string.IsNullOrEmpty(email) ? "" : $"{nameof(User)}.{email}";
|
string.IsNullOrEmpty(email) ? "" : $"{nameof(User)}.{email}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
@@ -11,20 +11,16 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="Microsoft.AspNetCore.Http.Abstractions">
|
|
||||||
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="Microsoft.AspNetCore.Http.Features" />
|
|
||||||
<Reference Include="Microsoft.Extensions.Options">
|
<Reference Include="Microsoft.Extensions.Options">
|
||||||
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.Extensions.Options.dll</HintPath>
|
<HintPath>C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.Extensions.Options.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="LazyCache.AspNetCore" Version="2.4.0" />
|
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
|
<PackageReference Include="LazyCache.AspNetCore" Version="2.4.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user