mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 11:06: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 Azaion.Common;
|
||||
using Azaion.Common.Configs;
|
||||
using Azaion.Common.Database;
|
||||
using Azaion.Common.Entities;
|
||||
@@ -106,6 +107,7 @@ builder.Services.AddLazyCache();
|
||||
builder.Services.AddScoped<ICache, MemoryCache>();
|
||||
|
||||
builder.Services.AddValidatorsFromAssemblyContaining<RegisterUserValidator>();
|
||||
builder.Services.AddExceptionHandler<BusinessExceptionHandler>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user