Enhance query handling and diagnostics

Improved robustness and error handling in `BodyQueryBehavior`
and `HeaderQueryBehavior` by adding null checks, logging,
and introducing the `IsReturnedNoData` flag to track query
results. Updated `RecActionDto` to include the new flag for
better state management. Enhanced HTTP request construction
in `InvokeRecActionCommand` to handle `Body` and `Headers`
more reliably and log potential issues with unexecuted
behaviors. These changes improve resilience, traceability,
and diagnostics across the application.
This commit is contained in:
tekh 2025-12-01 09:55:06 +01:00
commit 29f0a82f0f
4 changed files with 36 additions and 2 deletions

View File

@ -18,13 +18,24 @@ public class HeaderQueryBehavior<TRecAction>(IRecDbContext dbContext, ILogger<He
var result = await dbContext.HeaderQueryResults.FromSqlRaw(action.HeaderQuery).SingleOrDefaultAsync(cancel);
if(result?.RawHeader is null)
{
logger?.LogWarning("Header query did not return a result or returned a null REQUEST_HEADER. Profile ID: {ProfileId}, Action ID: {ActionId}", action.ProfileId, action.ActionId);
action.IsReturnedNoData.HeaderQuery = true;
return await next(cancel);
}
var headerDict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(result.RawHeader);
if(headerDict is null)
{
logger?.LogWarning("Failed to deserialize header query result: {RawHeader}. Profile ID: {ProfileId}, Action ID: {ActionId}", result.RawHeader, action.ProfileId, action.ActionId);
logger?.LogWarning(
"Header JSON deserialization returned null. RawHeader: {RawHeader}, ProfileId: {ProfileId}, ActionId: {ActionId}",
result.RawHeader, action.ProfileId, action.ActionId);
action.IsReturnedNoData.HeaderQuery = true;
return await next(cancel);
}

View File

@ -60,6 +60,8 @@ public record RecActionDto
public string? Body { get; set; }
public (bool BodyQuery, bool HeaderQuery) IsReturnedNoData = (false, false);
public string? PostprocessingQuery { get; init; }
public UriBuilder ToEndpointUriBuilder()

View File

@ -40,6 +40,27 @@ public class InvokeRecActionCommandHandler(
.ToHttpMethod()
.ToHttpRequestMessage(request.EndpointUri);
if(request.Body is not null)
{
using var reqBody = new StringContent(request.Body);
httpReq.Content = reqBody;
}
else if(request.BodyQuery is not null && !request.IsReturnedNoData.BodyQuery)
{
logger?.LogWarning(
"Although BodyQuery returns null, the IsReturnedNoData variable has not been set to TRUE. " +
"This indicates that BodyQueryBehavior has not been executed or that the relevant control step has been skipped. " +
"The relevant behavior must be verified to ensure that the process does not produce unexpected conditions. " +
"ProfileId: {ProfileId}, ActionId: {ActionId}",
request.ProfileId,
request.ActionId
);
}
if (request.Headers is not null)
foreach (var header in request.Headers)
httpReq.Headers.Add(header.Key, header.Value);
using var response = await http.SendAsync(httpReq, cancel);
var body = await response.Content.ReadAsStringAsync(cancel);
var headers = response.Headers.ToDictionary();