add correct business exception handling

use <FrameworkReference Include="Microsoft.AspNetCore.App" /> instead of old nuget packages
This commit is contained in:
Alex Bezdieniezhnykh
2025-04-16 13:32:16 +03:00
parent 4fc1fb4d63
commit b1693b2894
7 changed files with 45 additions and 12 deletions
+27
View File
@@ -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;
}
}