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:
@@ -106,6 +106,23 @@ public class ExceptionHandlingMiddleware
|
|||||||
};
|
};
|
||||||
break;
|
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:
|
default:
|
||||||
logger.LogError(exception, "Unhandled exception occurred.");
|
logger.LogError(exception, "Unhandled exception occurred.");
|
||||||
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||||
@@ -120,4 +137,4 @@ public class ExceptionHandlingMiddleware
|
|||||||
if (details is not null)
|
if (details is not null)
|
||||||
await context.Response.WriteAsJsonAsync(details);
|
await context.Response.WriteAsJsonAsync(details);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,20 +4,20 @@ namespace ReC.Application.Common.Exceptions;
|
|||||||
|
|
||||||
public class InsertObjectFailedException : Exception
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user