Refactor HttpExceptionHandler to use properties

Changed DefaultBadRequest, DefaultNotFound, and Default
from readonly fields to properties for lazy evaluation,
enhancing performance and flexibility while maintaining
the same functionality.
This commit is contained in:
Developer 02 2025-05-19 16:18:05 +02:00
parent 2c393701e4
commit fd8e976e1e

View File

@ -26,11 +26,11 @@ public record HttpExceptionHandler(Type ExceptionType, Func<HttpContext, Excepti
#region Default handlers #region Default handlers
public static readonly Func<Exception, string> DefaultMessageFactory = ex => ex.Message; public static readonly Func<Exception, string> DefaultMessageFactory = ex => ex.Message;
public static readonly HttpExceptionHandler DefaultBadRequest = Create<BadRequestException>(HttpStatusCode.BadRequest, DefaultMessageFactory); public static HttpExceptionHandler DefaultBadRequest => Create<BadRequestException>(HttpStatusCode.BadRequest, DefaultMessageFactory);
public static readonly HttpExceptionHandler DefaultNotFound = Create<NotFoundException>(HttpStatusCode.NotFound, DefaultMessageFactory); public static HttpExceptionHandler DefaultNotFound => Create<NotFoundException>(HttpStatusCode.NotFound, DefaultMessageFactory);
public static readonly HttpExceptionHandler Default = Create<Exception>( public static HttpExceptionHandler Default => Create<Exception>(
async (context, ex, logger) => async (context, ex, logger) =>
{ {
context.Response.ContentType = "application/json"; context.Response.ContentType = "application/json";