mirror of
https://github.com/azaion/admin.git
synced 2026-04-23 01:26:33 +00:00
add exceptions handler
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
@@ -106,6 +107,7 @@ builder.Services.AddLazyCache();
|
|||||||
builder.Services.AddScoped<ICache, MemoryCache>();
|
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();
|
||||||
|
|
||||||
@@ -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();
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user