Refactor PreprocessingBehavior to log results and errors

Refactored PreprocessingBehavior to serialize and log the results and errors of preprocessing SQL queries using InsertResultCommand. Now records both successful and failed executions, while preserving the stop-on-error behavior. Cleaned up and updated using directives.
This commit is contained in:
2026-03-24 14:01:45 +01:00
parent 35e99d9f2a
commit 9410c5dc0d

View File

@@ -1,8 +1,9 @@
using DigitalData.Core.Abstraction.Application.Repository; using MediatR;
using MediatR;
using ReC.Application.Common.Interfaces; using ReC.Application.Common.Interfaces;
using ReC.Application.RecActions.Commands; using ReC.Application.RecActions.Commands;
using ReC.Application.Results.Commands;
using ReC.Domain.Constants; using ReC.Domain.Constants;
using System.Text.Json;
namespace ReC.Application.Common.Behaviors.Action; namespace ReC.Application.Common.Behaviors.Action;
@@ -10,20 +11,31 @@ public class PreprocessingBehavior(IRecDbContext context, ISender sender) : IPip
{ {
public async Task<bool> Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate<bool> next, CancellationToken cancel) public async Task<bool> Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate<bool> next, CancellationToken cancel)
{ {
string? info = null, error = null;
try try
{ {
if (request.Action.PreprocessingQuery is string query) if (request.Action.PreprocessingQuery is string query)
await context.ExecuteDynamicSqlAsync(query, cancel); {
var result = await context.ExecuteDynamicSqlAsync(query, cancel);
info = JsonSerializer.Serialize(result);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
if(request.Action.ErrorAction == ErrorAction.Stop) error = ex.ToString();
{
// save output result
return false;
}
} }
await sender.Send(new InsertResultCommand()
{
ActionId = request.Action.Id,
Info = info,
Error = error
}, cancel);
if (error is not null && request.Action.ErrorAction == ErrorAction.Stop)
return false;
return await next(cancel); return await next(cancel);
} }
} }