From f586e9eb2fe5b72022bce862c0b862a9577d8ac9 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 19 May 2025 15:01:50 +0200 Subject: [PATCH] Refactor exception handling to use HttpExceptionHandler Updated GlobalExceptionHandlerOptions to replace HttpExceptionMapping with HttpExceptionHandler. Removed HttpExceptionMapping class and introduced HttpExceptionHandler for managing exceptions in HTTP requests. Added methods for creating specific exception handlers and default handlers for common exceptions, with JSON serialization for error messages. --- .../GlobalExceptionHandlerOptions.cs | 10 +++---- .../HttpExceptionHandler.cs | 29 +++++++++++++++++++ .../HttpExceptionMapping.cs | 15 ---------- 3 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 DigitalData.Core.Exceptions.Middleware/HttpExceptionHandler.cs delete mode 100644 DigitalData.Core.Exceptions.Middleware/HttpExceptionMapping.cs diff --git a/DigitalData.Core.Exceptions.Middleware/GlobalExceptionHandlerOptions.cs b/DigitalData.Core.Exceptions.Middleware/GlobalExceptionHandlerOptions.cs index 9a75e79..5f4b83a 100644 --- a/DigitalData.Core.Exceptions.Middleware/GlobalExceptionHandlerOptions.cs +++ b/DigitalData.Core.Exceptions.Middleware/GlobalExceptionHandlerOptions.cs @@ -2,16 +2,16 @@ public class GlobalExceptionHandlerOptions { - internal readonly Dictionary RegisteredMappings = new(); + internal readonly Dictionary Handlers = new(); - internal HttpExceptionMapping? DefaultMapping { get; private set; } + internal HttpExceptionHandler? DefaultHandler { get; private set; } - public GlobalExceptionHandlerOptions Add(HttpExceptionMapping mapping, bool setAsDefault = false) + public GlobalExceptionHandlerOptions Add(HttpExceptionHandler handler, bool setAsDefault = false) { if (setAsDefault) - DefaultMapping = mapping; + DefaultHandler = handler; else - RegisteredMappings[mapping.ExceptionType] = mapping; + Handlers[handler.ExceptionType] = handler; return this; } } \ No newline at end of file diff --git a/DigitalData.Core.Exceptions.Middleware/HttpExceptionHandler.cs b/DigitalData.Core.Exceptions.Middleware/HttpExceptionHandler.cs new file mode 100644 index 0000000..817d04e --- /dev/null +++ b/DigitalData.Core.Exceptions.Middleware/HttpExceptionHandler.cs @@ -0,0 +1,29 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using System.Net; +using System.Text.Json; + +namespace DigitalData.Core.Exceptions.Middleware; + +public record HttpExceptionHandler(Type ExceptionType, Func HandleExceptionAsync) +{ + public static HttpExceptionHandler Create(Func HandleExceptionAsync) where TException : Exception + => new HttpExceptionHandler(typeof(TException), HandleExceptionAsync); + + public static HttpExceptionHandler Create(HttpStatusCode statusCode, Func messageFactory) where TException : Exception + => Create( + async (context, ex, logger) => + { + context.Response.ContentType = "application/json"; + context.Response.StatusCode = (int)statusCode; + var message = messageFactory(ex); + await context.Response.WriteAsync(JsonSerializer.Serialize(new { message })); + } + ); + + public static readonly Func DefaultMessageFactory = ex => ex.Message; + + public static readonly HttpExceptionHandler DefaultBadRequest = Create(HttpStatusCode.BadRequest, DefaultMessageFactory); + + public static readonly HttpExceptionHandler DefaultNotFound = Create(HttpStatusCode.NotFound, DefaultMessageFactory); +}; \ No newline at end of file diff --git a/DigitalData.Core.Exceptions.Middleware/HttpExceptionMapping.cs b/DigitalData.Core.Exceptions.Middleware/HttpExceptionMapping.cs deleted file mode 100644 index 153f92e..0000000 --- a/DigitalData.Core.Exceptions.Middleware/HttpExceptionMapping.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Net; - -namespace DigitalData.Core.Exceptions.Middleware; - -public record HttpExceptionMapping(Type ExceptionType, HttpStatusCode StatusCode, Func? MessageFactory = null, bool Log = false) -{ - public static HttpExceptionMapping Create(HttpStatusCode statusCode, Func? messageFactory = null) where TException : Exception - => new HttpExceptionMapping(typeof(TException), statusCode, messageFactory); - - public static readonly Func? DefaultMessageFactory = ex => ex.Message; - - public static readonly HttpExceptionMapping DefaultBadRequest = Create(HttpStatusCode.BadRequest, DefaultMessageFactory); - - public static readonly HttpExceptionMapping DefaultNotFound = Create(HttpStatusCode.NotFound, DefaultMessageFactory); -}; \ No newline at end of file