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 MediatR;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using ReC.Application.Common.Dto;
|
using ReC.Application.Common.Dto;
|
||||||
|
using ReC.Application.Common.Exceptions;
|
||||||
using ReC.Application.Common.Interfaces;
|
using ReC.Application.Common.Interfaces;
|
||||||
|
|
||||||
namespace ReC.Application.Common.Behaviors.Action;
|
namespace ReC.Application.Common.Behaviors.Action;
|
||||||
@@ -31,7 +32,10 @@ public class BodyQueryBehavior<TRequest, TResponse>(IRecDbContext dbContext) : I
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var scalar = await command.ExecuteScalarAsync(cancel);
|
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
|
finally
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using ReC.Application.Common.Dto;
|
using ReC.Application.Common.Dto;
|
||||||
|
using ReC.Application.Common.Exceptions;
|
||||||
using ReC.Application.Common.Interfaces;
|
using ReC.Application.Common.Interfaces;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace ReC.Application.Common.Behaviors.Action;
|
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 TRequest : notnull
|
||||||
where TResponse : IEnumerable<RecActionViewDto>
|
where TResponse : IEnumerable<RecActionViewDto>
|
||||||
{
|
{
|
||||||
@@ -35,20 +35,12 @@ public class HeaderQueryBehavior<TRequest, TResponse>(IRecDbContext dbContext, I
|
|||||||
var scalar = await command.ExecuteScalarAsync(cancel);
|
var scalar = await command.ExecuteScalarAsync(cancel);
|
||||||
|
|
||||||
if (scalar is not string rawHeader)
|
if (scalar is not string rawHeader)
|
||||||
{
|
throw new DataIntegrityException(
|
||||||
logger?.LogWarning("Header query did not return a result or returned a null value. Profile ID: {ProfileId}, Action ID: {Id}", action.ProfileId, action.Id);
|
$"Header query returned no result or a null value. ActionId: {action.Id}, ProfileId: {action.ProfileId}");
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var headerDict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(rawHeader);
|
var headerDict = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(rawHeader)
|
||||||
|
?? throw new DataIntegrityException(
|
||||||
if (headerDict is null)
|
$"Header query returned invalid JSON. ActionId: {action.Id}, ProfileId: {action.ProfileId}");
|
||||||
{
|
|
||||||
logger?.LogWarning(
|
|
||||||
"Header JSON deserialization returned null. RawHeader: {RawHeader}, ProfileId: {ProfileId}, Id: {Id}",
|
|
||||||
rawHeader, action.ProfileId, action.Id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
action.Headers = headerDict.ToDictionary(header => header.Key, kvp => kvp.Value.ToString());
|
action.Headers = headerDict.ToDictionary(header => header.Key, kvp => kvp.Value.ToString());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user