Handle InsertObjectFailedException in middleware

Add specific handling for InsertObjectFailedException in ExceptionHandlingMiddleware, including detailed logging and custom error response. Refactor InsertObjectFailedException to expose the request data via a public property for improved error reporting.
This commit is contained in:
2026-01-14 09:51:40 +01:00
parent 24f146ca26
commit 0dedb506e1
2 changed files with 25 additions and 8 deletions

View File

@@ -106,6 +106,23 @@ public class ExceptionHandlingMiddleware
};
break;
case InsertObjectFailedException insertFailedEx:
logger.LogError(
insertFailedEx,
"Insert operation failed during request processing. {request}",
JsonSerializer.Serialize(
insertFailedEx.Request,
options: new() { WriteIndented = true }
));
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
details = new()
{
Title = "Insert Operation Failed",
Detail = insertFailedEx.Message
};
break;
default:
logger.LogError(exception, "Unhandled exception occurred.");
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
@@ -120,4 +137,4 @@ public class ExceptionHandlingMiddleware
if (details is not null)
await context.Response.WriteAsJsonAsync(details);
}
}
}

View File

@@ -4,20 +4,20 @@ namespace ReC.Application.Common.Exceptions;
public class InsertObjectFailedException : Exception
{
private readonly InsertObjectProcedure _procedure;
public InsertObjectProcedure Request { get; }
public InsertObjectFailedException(InsertObjectProcedure procedure) : base()
public InsertObjectFailedException(InsertObjectProcedure request) : base()
{
_procedure = procedure;
Request = request;
}
public InsertObjectFailedException(InsertObjectProcedure procedure, string? message) : base(message)
public InsertObjectFailedException(InsertObjectProcedure request, string? message) : base(message)
{
_procedure = procedure;
Request = request;
}
public InsertObjectFailedException(InsertObjectProcedure procedure, string? message, Exception? innerException) : base(message, innerException)
public InsertObjectFailedException(InsertObjectProcedure request, string? message, Exception? innerException) : base(message, innerException)
{
_procedure = procedure;
Request = request;
}
}