using System.Net;
using Jdb.Api.DTOs;
namespace Jdb.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 exception)
{
_logger.LogError(exception, "Unhandled request error");
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
await context.Response.WriteAsJsonAsync(new ApiResponse<object>
{
Code = StatusCodes.Status500InternalServerError,
Data = null,
Message = "Erro interno no servidor."
});
}
}
}
}
|