Initial commit: HelpDesk ICHN - .NET 10 backend + Next.js 16 frontend

This commit is contained in:
Nurfog committed 2026-09-10 10:20:51 -03:00
commit 359f0caf85
90 files changed
+13395

No files matched your search

@@ -0,0 +1,56 @@
using System.Net;
using System.Text.Json;
namespace HelpDesk.API.Middleware;
public class ExceptionMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ExceptionMiddleware> _logger;
public ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> 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: {Message}", ex.Message);
await HandleExceptionAsync(context, ex);
}
}
private static async Task HandleExceptionAsync(HttpContext context, Exception exception)
{
context.Response.ContentType = "application/json";
var (statusCode, message) = exception switch
{
ArgumentException argEx => (HttpStatusCode.BadRequest, argEx.Message),
KeyNotFoundException keyEx => (HttpStatusCode.NotFound, keyEx.Message),
UnauthorizedAccessException => (HttpStatusCode.Unauthorized, "Unauthorized access."),
InvalidOperationException opEx => (HttpStatusCode.BadRequest, opEx.Message),
_ => (HttpStatusCode.InternalServerError, "An unexpected error occurred.")
};
context.Response.StatusCode = (int)statusCode;
var response = new
{
error = message,
statusCode = (int)statusCode
};
await context.Response.WriteAsync(JsonSerializer.Serialize(response, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}));
}
}