Add global exception handling middleware
Introduces `GlobalExceptionHandlerMiddleware` for managing exceptions in the request pipeline, logging them, and returning JSON error responses. A new project, `DigitalData.Core.Exceptions.Middleware`, is created to house this middleware and related classes. The `GlobalExceptionHandlerOptions` class allows for custom exception registration with specific HTTP status codes, while a new `HttpResponse` record encapsulates status codes and messages for structured responses. The middleware is registered in the `DependencyInjection` class for easy integration.
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
using System.Net;
|
||||
|
||||
namespace DigitalData.Core.Exceptions.Middleware;
|
||||
|
||||
public class GlobalExceptionHandlerOptions
|
||||
{
|
||||
private readonly Dictionary<Type, HttpResponse> _registeredExceptions = new();
|
||||
|
||||
public GlobalExceptionHandlerOptions RegisterException<TException>(HttpStatusCode statusCode, string? message = null) where TException : Exception
|
||||
{
|
||||
_registeredExceptions[typeof(TException)] = new HttpResponse(statusCode, message);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user