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