From d239d43c1c82120f5439828d6bb02419afee342e Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 3 Dec 2025 09:34:02 +0100 Subject: [PATCH] Add handling for DataIntegrityException in middleware Enhanced the ExceptionHandlingMiddleware to handle DataIntegrityException explicitly. Added a new `using` directive for `ReC.Application.Common.Exceptions` to support this change. When a DataIntegrityException is caught, the middleware now sets the HTTP status code to 409 Conflict and returns the exception's message. Also updated the default case to ensure unhandled exceptions are logged and return a generic error message with a 500 Internal Server Error status code. --- 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 dc02a81..0486ea6 100644 --- a/src/ReC.API/Middleware/ExceptionHandlingMiddleware.cs +++ b/src/ReC.API/Middleware/ExceptionHandlingMiddleware.cs @@ -1,4 +1,5 @@ using DigitalData.Core.Exceptions; +using ReC.Application.Common.Exceptions; using System.Net; using System.Text.Json; @@ -69,6 +70,11 @@ public class ExceptionHandlingMiddleware message = notFoundEx.Message; break; + case DataIntegrityException dataIntegrityEx: + context.Response.StatusCode = (int)HttpStatusCode.Conflict; + message = dataIntegrityEx.Message; + break; + default: logger.LogError(exception, "Unhandled exception occurred."); context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;