Stricter error handling in BodyQuery and HeaderQuery behaviors
Throw DataIntegrityException when body or header queries return null, no result, or invalid JSON. Remove ILogger and related logging from HeaderQueryBehavior for simplification. This change ensures data integrity issues are surfaced immediately.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ReC.Application.Common.Dto;
|
||||
using ReC.Application.Common.Exceptions;
|
||||
using ReC.Application.Common.Interfaces;
|
||||
|
||||
namespace ReC.Application.Common.Behaviors.Action;
|
||||
@@ -31,7 +32,10 @@ public class BodyQueryBehavior<TRequest, TResponse>(IRecDbContext dbContext) : I
|
||||
try
|
||||
{
|
||||
var scalar = await command.ExecuteScalarAsync(cancel);
|
||||
action.Body = scalar as string;
|
||||
|
||||
action.Body = scalar as string
|
||||
?? throw new DataIntegrityException(
|
||||
$"Body query returned no result or a null value. ActionId: {action.Id}, ProfileId: {action.ProfileId}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ReC.Application.Common.Dto;
|
||||
using ReC.Application.Common.Exceptions;
|
||||
using ReC.Application.Common.Interfaces;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ReC.Application.Common.Behaviors.Action;
|
||||
|
||||
public class HeaderQueryBehavior<TRequest, TResponse>(IRecDbContext dbContext, ILogger<HeaderQueryBehavior<TRequest, TResponse>>? logger = null) : IPipelineBehavior<TRequest, TResponse>
|
||||
public class HeaderQueryBehavior<TRequest, TResponse>(IRecDbContext dbContext) : IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : notnull
|
||||
where TResponse : IEnumerable<RecActionViewDto>
|
||||
{
|
||||
@@ -35,20 +35,12 @@ public class HeaderQueryBehavior<TRequest, TResponse>(IRecDbContext dbContext, I
|
||||
var scalar = await command.ExecuteScalarAsync(cancel);
|
||||
|
||||
if (scalar is not string rawHeader)
|
||||
{
|
||||
logger?.LogWarning("Header query did not return a result or returned a null value. Profile ID: {ProfileId}, Action ID: {Id}", action.ProfileId, action.Id);
|
||||
return;
|
||||
}
|
||||
throw new DataIntegrityException(
|
||||
$"Header query returned no result or a null value. ActionId: {action.Id}, ProfileId: {action.ProfileId}");
|
||||
|
||||
var headerDict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(rawHeader);
|
||||
|
||||
if (headerDict is null)
|
||||
{
|
||||
logger?.LogWarning(
|
||||
"Header JSON deserialization returned null. RawHeader: {RawHeader}, ProfileId: {ProfileId}, Id: {Id}",
|
||||
rawHeader, action.ProfileId, action.Id);
|
||||
return;
|
||||
}
|
||||
var headerDict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(rawHeader)
|
||||
?? throw new DataIntegrityException(
|
||||
$"Header query returned invalid JSON. ActionId: {action.Id}, ProfileId: {action.ProfileId}");
|
||||
|
||||
action.Headers = headerDict.ToDictionary(header => header.Key, kvp => kvp.Value.ToString());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user