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