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.
This commit is contained in:
@@ -1,14 +1,12 @@
|
||||
using System.Net;
|
||||
|
||||
namespace DigitalData.Core.Exceptions.Middleware;
|
||||
namespace DigitalData.Core.Exceptions.Middleware;
|
||||
|
||||
public class GlobalExceptionHandlerOptions
|
||||
{
|
||||
private readonly Dictionary<Type, HttpResponse> _registeredExceptions = new();
|
||||
private readonly Dictionary<Type, HttpExceptionMapping> _registeredMappings = new();
|
||||
|
||||
public GlobalExceptionHandlerOptions RegisterException<TException>(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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user