Improve error handling in query behaviors for SQL execution
Wrap ExecuteScalarAsync in try-catch blocks in BodyQueryBehavior and HeaderQueryBehavior. Throw DataIntegrityException with detailed context if SQL execution fails, aiding in diagnosing malformed or problematic stored SQL queries.
This commit is contained in:
@@ -31,7 +31,16 @@ public class BodyQueryBehavior<TRequest, TResponse>(IRecDbContext dbContext) : I
|
|||||||
await dbContext.Database.OpenConnectionAsync(cancel);
|
await dbContext.Database.OpenConnectionAsync(cancel);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var scalar = await command.ExecuteScalarAsync(cancel);
|
object? scalar;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
scalar = await command.ExecuteScalarAsync(cancel);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new DataIntegrityException(
|
||||||
|
$"Body query execution failed. The stored SQL may be malformed. ActionId: {action.Id}, ProfileId: {action.ProfileId}, Error: {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
action.Body = scalar as string
|
action.Body = scalar as string
|
||||||
?? throw new DataIntegrityException(
|
?? throw new DataIntegrityException(
|
||||||
|
|||||||
@@ -32,7 +32,16 @@ public class HeaderQueryBehavior<TRequest, TResponse>(IRecDbContext dbContext) :
|
|||||||
await dbContext.Database.OpenConnectionAsync(cancel);
|
await dbContext.Database.OpenConnectionAsync(cancel);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var scalar = await command.ExecuteScalarAsync(cancel);
|
object? scalar;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
scalar = await command.ExecuteScalarAsync(cancel);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new DataIntegrityException(
|
||||||
|
$"Header query execution failed. The stored SQL may be malformed. ActionId: {action.Id}, ProfileId: {action.ProfileId}, Error: {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
if (scalar is not string rawHeader)
|
if (scalar is not string rawHeader)
|
||||||
throw new DataIntegrityException(
|
throw new DataIntegrityException(
|
||||||
|
|||||||
Reference in New Issue
Block a user