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:
2026-04-17 00:34:01 +02:00
parent bb2dd4d63b
commit d149cbea3a
2 changed files with 20 additions and 2 deletions

View File

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

View File

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