WebApi + ClientApp, GraphQL, Reflection

This commit is contained in:
Boris Milašinović
2026-05-06 20:55:05 +02:00
parent 8f7c704a90
commit 4fb3de19f6
196 changed files with 10395 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
using FluentValidation;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Events.WebAPI.Util.Extensions;
using Events.WebAPI.Contract.Command;
namespace Events.WebAPI.Util.Middleware;
public class BadRequestOnRuleValidationException : ExceptionFilterAttribute
{
private readonly ILogger<BadRequestOnRuleValidationException> logger;
public BadRequestOnRuleValidationException(ILogger<BadRequestOnRuleValidationException> logger)
{
this.logger = logger;
}
public override void OnException(ExceptionContext context)
{
if (context.Exception is ValidationException)
{
string exceptionMessage = context.Exception.CompleteExceptionMessage();
logger.LogDebug("Validation error: {0}", exceptionMessage);
ValidationException exc = (ValidationException)context.Exception;
Dictionary<string, List<string>> validationErrors = new Dictionary<string, List<string>>();
Dictionary<string, List<string>> validationErrorCodes = new Dictionary<string, List<string>>();
foreach(var failure in exc.Errors)
{
//remove prefix Dto. (part of Update and AddCommand)
string propertyName = failure.PropertyName.Replace(nameof(AddCommand<object, object>.Dto) + ".", "");
if (propertyName == nameof(AddCommand<object, object>.Dto))
{
propertyName = string.Empty;
}
validationErrors.GetOrCreate(propertyName).Add(failure.ErrorMessage);
if (!string.IsNullOrWhiteSpace(failure.ErrorCode))
{
validationErrorCodes.GetOrCreate(propertyName).Add(failure.ErrorCode);
}
}
var problemDetails = new ValidationProblemDetails(validationErrors.ToDictionary(d => d.Key, d => d.Value.ToArray()))
{
Detail = context.Exception.Message,
Title = "Validation exception",
Instance = context.HttpContext.TraceIdentifier
};
if (validationErrorCodes.Count > 0)
{
problemDetails.Extensions["errorCodes"] = validationErrorCodes.ToDictionary(d => d.Key, d => d.Value.ToArray());
}
context.Result = new ObjectResult(problemDetails)
{
ContentTypes = { "application/problem+json" },
StatusCode = StatusCodes.Status400BadRequest
};
context.HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
context.ExceptionHandled = true;
}
}
}