From 9b29a49ad68d62338034eecf092b6fdffe43d748 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 19 May 2025 14:15:38 +0200 Subject: [PATCH] Refactor exception handling with new mapping system Updated `GlobalExceptionHandlerOptions` to use a new `_registeredMappings` dictionary for `HttpExceptionMapping` objects, enhancing flexibility in mapping exceptions to HTTP responses. Renamed `RegisterException` to `Add` to reflect its new functionality. Removed the `HttpResponse` record definition and introduced a new `HttpExceptionMapping` record, which includes a static `Create` method for easier instantiation of mappings. --- .../GlobalExceptionHandlerOptions.cs | 10 ++++------ .../HttpExceptionMapping.cs | 9 +++++++++ DigitalData.Core.Exceptions.Middleware/HttpResponse.cs | 5 ----- 3 files changed, 13 insertions(+), 11 deletions(-) create mode 100644 DigitalData.Core.Exceptions.Middleware/HttpExceptionMapping.cs delete mode 100644 DigitalData.Core.Exceptions.Middleware/HttpResponse.cs diff --git a/DigitalData.Core.Exceptions.Middleware/GlobalExceptionHandlerOptions.cs b/DigitalData.Core.Exceptions.Middleware/GlobalExceptionHandlerOptions.cs index 239425b..d051d96 100644 --- a/DigitalData.Core.Exceptions.Middleware/GlobalExceptionHandlerOptions.cs +++ b/DigitalData.Core.Exceptions.Middleware/GlobalExceptionHandlerOptions.cs @@ -1,14 +1,12 @@ -using System.Net; - -namespace DigitalData.Core.Exceptions.Middleware; +namespace DigitalData.Core.Exceptions.Middleware; public class GlobalExceptionHandlerOptions { - private readonly Dictionary _registeredExceptions = new(); + private readonly Dictionary _registeredMappings = new(); - public GlobalExceptionHandlerOptions RegisterException(HttpStatusCode statusCode, string? message = null) where TException : Exception + public GlobalExceptionHandlerOptions Add(HttpExceptionMapping mapping) { - _registeredExceptions[typeof(TException)] = new HttpResponse(statusCode, message); + _registeredMappings[mapping.ExceptionType] = mapping; return this; } } \ No newline at end of file diff --git a/DigitalData.Core.Exceptions.Middleware/HttpExceptionMapping.cs b/DigitalData.Core.Exceptions.Middleware/HttpExceptionMapping.cs new file mode 100644 index 0000000..e87c45c --- /dev/null +++ b/DigitalData.Core.Exceptions.Middleware/HttpExceptionMapping.cs @@ -0,0 +1,9 @@ +using System.Net; + +namespace DigitalData.Core.Exceptions.Middleware; + +public record HttpExceptionMapping(Type ExceptionType, HttpStatusCode StatusCode, Func? MessageFactory = null) +{ + public static HttpExceptionMapping Create(HttpStatusCode statusCode, Func? messageFactory = null) where TException : Exception + => new HttpExceptionMapping(typeof(TException), statusCode, messageFactory); +}; \ No newline at end of file diff --git a/DigitalData.Core.Exceptions.Middleware/HttpResponse.cs b/DigitalData.Core.Exceptions.Middleware/HttpResponse.cs deleted file mode 100644 index 37002ea..0000000 --- a/DigitalData.Core.Exceptions.Middleware/HttpResponse.cs +++ /dev/null @@ -1,5 +0,0 @@ -using System.Net; - -namespace DigitalData.Core.Exceptions.Middleware; - -public record HttpResponse(HttpStatusCode StatusCode, string? Message = null); \ No newline at end of file