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.
This commit is contained in:
tekh 2025-12-03 09:34:02 +01:00
parent 586b99ddc7
commit d239d43c1c

View File

@ -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;