#region ========================================================================= USING ===================================================================================== using System; using ErrorOr; using System.Linq; using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using System.Collections.Generic; using Microsoft.Extensions.Options; using Lumina.Presentation.Api.Common.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.Infrastructure; #endregion namespace Lumina.Presentation.Api.Common.Errors; /// /// Problem details abstract factory which adds custom properties to the default implementation provided by Microsoft /// /// /// Creation Date: 14th of July, 2023 /// public class CustomProblemDetailsFactory : ProblemDetailsFactory { #region ================================================================== FIELD MEMBERS ================================================================================ private readonly ApiBehaviorOptions options; private readonly Action? configure; #endregion #region ====================================================================== CTOR ===================================================================================== /// /// Overload C-tor /// /// Injected service for retrieving ApiBehaviorOptions /// Injected service for retrieving ProblemDetailsOptions /// Thrown when the value of options (if any) is null public CustomProblemDetailsFactory(IOptions options, IOptions? problemDetailsOptions = null) { this.options = options?.Value ?? throw new ArgumentNullException(nameof(options)); configure = problemDetailsOptions?.Value?.CustomizeProblemDetails; } #endregion #region ===================================================================== METHODS =================================================================================== /// /// Creates a instance that configures defaults based on values specified in . /// /// The . /// The value for . /// The value for . /// The value for . /// The value for . /// The value for . /// The instance. public override ProblemDetails CreateProblemDetails(HttpContext httpContext, int? statusCode = null, string? title = null, string? type = null, string? detail = null, string? instance = null) { statusCode ??= 500; var problemDetails = new ProblemDetails { Status = statusCode, Title = title, Type = type, Detail = detail, Instance = instance, }; ApplyProblemDetailsDefaults(httpContext, problemDetails, statusCode.Value); return problemDetails; } /// /// Creates a instance that configures defaults based on values specified in . /// /// The . /// The . /// The value for . /// The value for . /// The value for . /// The value for . /// The value for . /// The instance. public override ValidationProblemDetails CreateValidationProblemDetails(HttpContext httpContext, ModelStateDictionary modelStateDictionary, int? statusCode = null, string? title = null, string? type = null, string? detail = null, string? instance = null) { ArgumentNullException.ThrowIfNull(modelStateDictionary); statusCode ??= 400; var problemDetails = new ValidationProblemDetails(modelStateDictionary) { Status = statusCode, Type = type, Detail = detail, Instance = instance, }; if (title != null) problemDetails.Title = title; // for validation problem details, don't overwrite the default title with null ApplyProblemDetailsDefaults(httpContext, problemDetails, statusCode.Value); return problemDetails; } /// /// Creates a instance that configures defaults based on values specified in . /// /// The . /// The value for . /// The value for . /// The instance. private void ApplyProblemDetailsDefaults(HttpContext httpContext, ProblemDetails problemDetails, int statusCode) { problemDetails.Status ??= statusCode; if (options.ClientErrorMapping.TryGetValue(statusCode, out var clientErrorData)) { problemDetails.Title ??= clientErrorData.Title; problemDetails.Type ??= clientErrorData.Link; } var traceId = Activity.Current?.Id ?? httpContext?.TraceIdentifier; if (traceId != null) problemDetails.Extensions["traceId"] = traceId; // get any errors that might be sent by ApiController base class' Problem() method var errors = httpContext?.Items[HttpContextItemKeys.ERRORS] as List; // add any extra custom properties if (errors is not null) { problemDetails.Extensions.Add("errors", errors.Select(e => { return e.Code; })); } //problemDetails.Extensions.Add("errorCodes", errors.Select(e => e.Type.ToString())); configure?.Invoke(new() { HttpContext = httpContext!, ProblemDetails = problemDetails }); } #endregion }