From 2c70cb441d8c8eb73b977fd868fe022a442cd0ce Mon Sep 17 00:00:00 2001 From: Alejandro Sarmiento Date: Tue, 20 Feb 2024 01:13:12 +0100 Subject: [PATCH] =?UTF-8?q?Exception=20Middleware=20A=C3=B1adido=20y=20fun?= =?UTF-8?q?cionando=20de=20aquella=20manera?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Errors/CodeErrorException.cs | 13 ++++++ .../Errors/CodeErrorResponse.cs | 26 +++++++++++ .../Middlewares/ExceptionMiddleware.cs | 44 +++++++++++++++++++ .../CleanArchitecture.API/Program.cs | 3 ++ 4 files changed, 86 insertions(+) create mode 100644 CleanArchitecture/CleanArchitecture.API/Errors/CodeErrorException.cs create mode 100644 CleanArchitecture/CleanArchitecture.API/Errors/CodeErrorResponse.cs create mode 100644 CleanArchitecture/CleanArchitecture.API/Middlewares/ExceptionMiddleware.cs diff --git a/CleanArchitecture/CleanArchitecture.API/Errors/CodeErrorException.cs b/CleanArchitecture/CleanArchitecture.API/Errors/CodeErrorException.cs new file mode 100644 index 0000000..e665036 --- /dev/null +++ b/CleanArchitecture/CleanArchitecture.API/Errors/CodeErrorException.cs @@ -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; + } + } +} diff --git a/CleanArchitecture/CleanArchitecture.API/Errors/CodeErrorResponse.cs b/CleanArchitecture/CleanArchitecture.API/Errors/CodeErrorResponse.cs new file mode 100644 index 0000000..9ba1ef0 --- /dev/null +++ b/CleanArchitecture/CleanArchitecture.API/Errors/CodeErrorResponse.cs @@ -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 + }; + } + } +} diff --git a/CleanArchitecture/CleanArchitecture.API/Middlewares/ExceptionMiddleware.cs b/CleanArchitecture/CleanArchitecture.API/Middlewares/ExceptionMiddleware.cs new file mode 100644 index 0000000..75a6327 --- /dev/null +++ b/CleanArchitecture/CleanArchitecture.API/Middlewares/ExceptionMiddleware.cs @@ -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 _logger; + private readonly IWebHostEnvironment _env; + + public ExceptionMiddleware(RequestDelegate next, ILogger 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); + + } + } + } +} diff --git a/CleanArchitecture/CleanArchitecture.API/Program.cs b/CleanArchitecture/CleanArchitecture.API/Program.cs index f1f8b0b..24c6b45 100644 --- a/CleanArchitecture/CleanArchitecture.API/Program.cs +++ b/CleanArchitecture/CleanArchitecture.API/Program.cs @@ -1,6 +1,7 @@ using CleanArchitecture.Application; using CleanArchitecture.Infrastructure; using CleanArchitecture.Identity; +using CleanArchitecture.API.Middlewares; var builder = WebApplication.CreateBuilder(args); @@ -34,6 +35,8 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } +app.UseMiddleware(); + app.UseAuthentication(); app.UseAuthorization();