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 HandleExceptionAsync) { #region Alternative generator methods public static HttpExceptionHandler Create(Func HandleExceptionAsync) where TException : Exception => new HttpExceptionHandler(typeof(TException), HandleExceptionAsync); public static HttpExceptionHandler Create(HttpStatusCode statusCode, Func messageFactory) where TException : Exception => Create( 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 })); } ); #endregion #region Default handlers public static readonly Func DefaultMessageFactory = ex => ex.Message; public static HttpExceptionHandler DefaultBadRequest => Create(HttpStatusCode.BadRequest, DefaultMessageFactory); public static HttpExceptionHandler DefaultNotFound => Create(HttpStatusCode.NotFound, DefaultMessageFactory); public static HttpExceptionHandler Default => Create( async (context, ex, logger) => { context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; var message = "An unexpected error occurred."; await context.Response.WriteAsync(JsonSerializer.Serialize(new { message })); }); #endregion };