refactor: rename namespace to FiscalFlow and upgrade to .NET 10
- Rename InvoiceMaster.* to FiscalFlow.* namespace - Upgrade from .NET 8 to .NET 10 - Update all NuGet packages to latest versions - Update C# language version to 14.0
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
|
||||
namespace FiscalFlow.API.Middleware;
|
||||
|
||||
public class ExceptionHandlingMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
|
||||
|
||||
public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "An unhandled exception occurred");
|
||||
await HandleExceptionAsync(context, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||
{
|
||||
context.Response.ContentType = "application/json";
|
||||
|
||||
var (statusCode, errorCode, message) = exception switch
|
||||
{
|
||||
UnauthorizedAccessException _ => (
|
||||
(int)HttpStatusCode.Unauthorized,
|
||||
"UNAUTHORIZED",
|
||||
"Authentication required"
|
||||
),
|
||||
InvalidOperationException _ => (
|
||||
(int)HttpStatusCode.BadRequest,
|
||||
"INVALID_OPERATION",
|
||||
exception.Message
|
||||
),
|
||||
KeyNotFoundException _ => (
|
||||
(int)HttpStatusCode.NotFound,
|
||||
"NOT_FOUND",
|
||||
"Resource not found"
|
||||
),
|
||||
_ => (
|
||||
(int)HttpStatusCode.InternalServerError,
|
||||
"INTERNAL_ERROR",
|
||||
"An unexpected error occurred"
|
||||
)
|
||||
};
|
||||
|
||||
context.Response.StatusCode = statusCode;
|
||||
|
||||
var response = new
|
||||
{
|
||||
success = false,
|
||||
error = new
|
||||
{
|
||||
code = errorCode,
|
||||
message
|
||||
},
|
||||
meta = new
|
||||
{
|
||||
request_id = context.TraceIdentifier,
|
||||
timestamp = DateTime.UtcNow.ToString("O")
|
||||
}
|
||||
};
|
||||
|
||||
return context.Response.WriteAsJsonAsync(response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user