Refactor error handling in Pre/Postprocessing behaviors

Refactored PreprocessingBehavior and PostprocessingBehavior to send InsertResultCommand with only relevant info or error fields. Now throws exceptions when ErrorAction is Stop, allowing upstream error handling and preventing unnecessary or misleading result data. Improves consistency and clarity in error processing.
This commit is contained in:
2026-03-24 16:19:00 +01:00
parent 2ca85a2372
commit a707cce6e4
2 changed files with 32 additions and 34 deletions

View File

@@ -11,37 +11,35 @@ public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPi
{
public async Task<Unit> Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate<Unit> next, CancellationToken cancel)
{
try
{
await next(cancel);
}
catch
{
if (request.Action.ErrorAction == ErrorAction.Stop)
return Unit.Value;
}
string? info = null, error = null;
await next(cancel);
try
{
if (request.Action.PostprocessingQuery is string query)
{
var result = await context.ExecuteDynamicSqlAsync(query, cancel);
info = JsonSerializer.Serialize(result);
var info = JsonSerializer.Serialize(result);
await sender.Send(new InsertResultCommand()
{
ActionId = request.Action.Id,
Info = info
}, cancel);
}
}
catch (Exception ex)
{
error = ex.ToString();
}
var error = ex.ToString();
await sender.Send(new InsertResultCommand()
{
ActionId = request.Action.Id,
Info = info,
Error = error
}, cancel);
await sender.Send(new InsertResultCommand()
{
ActionId = request.Action.Id,
Error = error
}, cancel);
if (request.Action.ErrorAction == ErrorAction.Stop)
throw;
}
return Unit.Value;
}

View File

@@ -11,31 +11,31 @@ public class PreprocessingBehavior(IRecDbContext context, ISender sender) : IPip
{
public async Task<Unit> Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate<Unit> next, CancellationToken cancel)
{
string? info = null, error = null;
try
{
if (request.Action.PreprocessingQuery is string query)
{
var result = await context.ExecuteDynamicSqlAsync(query, cancel);
info = JsonSerializer.Serialize(result);
await sender.Send(new InsertResultCommand()
{
ActionId = request.Action.Id,
Info = JsonSerializer.Serialize(result)
}, cancel);
}
}
catch (Exception ex)
{
error = ex.ToString();
await sender.Send(new InsertResultCommand()
{
ActionId = request.Action.Id,
Error = ex.ToString()
}, cancel);
if (request.Action.ErrorAction == ErrorAction.Stop)
throw;
}
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 Unit.Value;
return await next(cancel);
}
}