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.
This commit is contained in:
Developer 02 2025-05-19 15:01:50 +02:00
parent ce786a6d42
commit f586e9eb2f
3 changed files with 34 additions and 20 deletions

View File

@ -2,16 +2,16 @@
public class GlobalExceptionHandlerOptions
{
internal readonly Dictionary<Type, HttpExceptionMapping> RegisteredMappings = new();
internal readonly Dictionary<Type, HttpExceptionHandler> 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;
}
}

View File

@ -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<HttpContext, Exception, ILogger?, Task> HandleExceptionAsync)
{
public static HttpExceptionHandler Create<TException>(Func<HttpContext, Exception, ILogger?, Task> HandleExceptionAsync) where TException : Exception
=> new HttpExceptionHandler(typeof(TException), HandleExceptionAsync);
public static HttpExceptionHandler Create<TException>(HttpStatusCode statusCode, Func<Exception, string> messageFactory) where TException : Exception
=> Create<TException>(
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<Exception, string> DefaultMessageFactory = ex => ex.Message;
public static readonly HttpExceptionHandler DefaultBadRequest = Create<BadRequestException>(HttpStatusCode.BadRequest, DefaultMessageFactory);
public static readonly HttpExceptionHandler DefaultNotFound = Create<NotFoundException>(HttpStatusCode.NotFound, DefaultMessageFactory);
};

View File

@ -1,15 +0,0 @@
using System.Net;
namespace DigitalData.Core.Exceptions.Middleware;
public record HttpExceptionMapping(Type ExceptionType, HttpStatusCode StatusCode, Func<Exception, string>? MessageFactory = null, bool Log = false)
{
public static HttpExceptionMapping Create<TException>(HttpStatusCode statusCode, Func<Exception, string>? messageFactory = null) where TException : Exception
=> new HttpExceptionMapping(typeof(TException), statusCode, messageFactory);
public static readonly Func<Exception, string>? DefaultMessageFactory = ex => ex.Message;
public static readonly HttpExceptionMapping DefaultBadRequest = Create<BadRequestException>(HttpStatusCode.BadRequest, DefaultMessageFactory);
public static readonly HttpExceptionMapping DefaultNotFound = Create<NotFoundException>(HttpStatusCode.NotFound, DefaultMessageFactory);
};