From 459cc1cb9ad156cc1fb9bb1bf12ea450e73b4370 Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 3 Dec 2025 15:03:22 +0100 Subject: [PATCH] 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. --- src/ReC.API/Middleware/ExceptionHandlingMiddleware.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ReC.API/Middleware/ExceptionHandlingMiddleware.cs b/src/ReC.API/Middleware/ExceptionHandlingMiddleware.cs index 0486ea6..c770712 100644 --- a/src/ReC.API/Middleware/ExceptionHandlingMiddleware.cs +++ b/src/ReC.API/Middleware/ExceptionHandlingMiddleware.cs @@ -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;