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:
parent
cd53d7fbae
commit
459cc1cb9a
@ -1,4 +1,5 @@
|
|||||||
using DigitalData.Core.Exceptions;
|
using DigitalData.Core.Exceptions;
|
||||||
|
using FluentValidation;
|
||||||
using ReC.Application.Common.Exceptions;
|
using ReC.Application.Common.Exceptions;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
@ -65,6 +66,11 @@ public class ExceptionHandlingMiddleware
|
|||||||
message = badRequestEx.Message;
|
message = badRequestEx.Message;
|
||||||
break;
|
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:
|
case NotFoundException notFoundEx:
|
||||||
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
|
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
|
||||||
message = notFoundEx.Message;
|
message = notFoundEx.Message;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user