Add ValidationException handling to middleware

Enhanced the `ExceptionHandlingMiddleware` to handle
`ValidationException` from the `FluentValidation` library.
Set HTTP status code to 422 (Unprocessable Entity) for
validation errors and constructed error messages by
aggregating validation error details. Improved error
response clarity for validation-related issues.
This commit is contained in:
tekh 2025-12-03 15:03:22 +01:00
parent cd53d7fbae
commit 459cc1cb9a

View File

@ -1,4 +1,5 @@
using DigitalData.Core.Exceptions;
using FluentValidation;
using ReC.Application.Common.Exceptions;
using System.Net;
using System.Text.Json;
@ -65,6 +66,11 @@ public class ExceptionHandlingMiddleware
message = badRequestEx.Message;
break;
case ValidationException validationEx:
context.Response.StatusCode = (int)HttpStatusCode.UnprocessableEntity;
message = "Validation failed: " + string.Join("; ", validationEx.Errors.Select(e => e.ErrorMessage));
break;
case NotFoundException notFoundEx:
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
message = notFoundEx.Message;