Exception Middleware Añadido y funcionando de aquella manera
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
namespace CleanArchitecture.API.Errors
|
||||||
|
{
|
||||||
|
public class CodeErrorException : CodeErrorResponse
|
||||||
|
{
|
||||||
|
|
||||||
|
public string? Details { get; set; }
|
||||||
|
public CodeErrorException(int statusCode, string? message = null, string? details = null) : base(statusCode, message)
|
||||||
|
{
|
||||||
|
|
||||||
|
Details = details;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
namespace CleanArchitecture.API.Errors
|
||||||
|
{
|
||||||
|
public class CodeErrorResponse
|
||||||
|
{
|
||||||
|
public int StatusCode { get; set; }
|
||||||
|
public string? Message { get; set; }
|
||||||
|
|
||||||
|
public CodeErrorResponse(int statusCode, string? message = null)
|
||||||
|
{
|
||||||
|
StatusCode = statusCode;
|
||||||
|
Message = message ?? GetDefaultMessageStatusCode(statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetDefaultMessageStatusCode(int statusCode)
|
||||||
|
{
|
||||||
|
return statusCode switch
|
||||||
|
{
|
||||||
|
400 => "The request sent have errors",
|
||||||
|
401 => "You are not authorized",
|
||||||
|
404 => "Resource not found",
|
||||||
|
500 => "An internal server error occurred",
|
||||||
|
_ => string.Empty
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
using CleanArchitecture.API.Errors;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace CleanArchitecture.API.Middlewares
|
||||||
|
{
|
||||||
|
public class ExceptionMiddleware
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next;
|
||||||
|
private readonly ILogger<ExceptionMiddleware> _logger;
|
||||||
|
private readonly IWebHostEnvironment _env;
|
||||||
|
|
||||||
|
public ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> logger, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
_next = next;
|
||||||
|
_logger = logger;
|
||||||
|
_env = env;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InvokeAsync(HttpContext context)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _next(context);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, ex.Message);
|
||||||
|
context.Response.ContentType = "application/json";
|
||||||
|
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||||
|
|
||||||
|
var response = _env.IsDevelopment() ?
|
||||||
|
new CodeErrorException((int)HttpStatusCode.InternalServerError, ex.Message, ex.StackTrace)
|
||||||
|
: new CodeErrorException((int)HttpStatusCode.InternalServerError);
|
||||||
|
|
||||||
|
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
|
||||||
|
var json = JsonSerializer.Serialize(response, options);
|
||||||
|
|
||||||
|
await context.Response.WriteAsync(json);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using CleanArchitecture.Application;
|
using CleanArchitecture.Application;
|
||||||
using CleanArchitecture.Infrastructure;
|
using CleanArchitecture.Infrastructure;
|
||||||
using CleanArchitecture.Identity;
|
using CleanArchitecture.Identity;
|
||||||
|
using CleanArchitecture.API.Middlewares;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@@ -34,6 +35,8 @@ if (app.Environment.IsDevelopment())
|
|||||||
app.UseSwaggerUI();
|
app.UseSwaggerUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.UseMiddleware<ExceptionMiddleware>();
|
||||||
|
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user