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:
commit
29f0a82f0f
@ -9,7 +9,7 @@ public class BodyQueryBehavior<TRecAction>(IRecDbContext dbContext) : IPipelineB
|
||||
where TRecAction : RecActionDto
|
||||
{
|
||||
public async Task<Unit> Handle(TRecAction action, RequestHandlerDelegate<Unit> next, CancellationToken cancel)
|
||||
{
|
||||
{
|
||||
if (action.BodyQuery is null)
|
||||
return await next(cancel);
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user