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:
@@ -10,39 +10,37 @@ namespace ReC.Application.Common.Behaviors.Action;
|
||||
public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPipelineBehavior<InvokeRecActionViewCommand, Unit>
|
||||
{
|
||||
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;
|
||||
|
||||
try
|
||||
{
|
||||
if (request.Action.PostprocessingQuery is string query)
|
||||
{
|
||||
var result = await context.ExecuteDynamicSqlAsync(query, cancel);
|
||||
info = JsonSerializer.Serialize(result);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
}
|
||||
var info = JsonSerializer.Serialize(result);
|
||||
|
||||
await sender.Send(new InsertResultCommand()
|
||||
{
|
||||
ActionId = request.Action.Id,
|
||||
Info = info
|
||||
}, cancel);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var error = ex.ToString();
|
||||
|
||||
await sender.Send(new InsertResultCommand()
|
||||
{
|
||||
ActionId = request.Action.Id,
|
||||
Info = info,
|
||||
Error = error
|
||||
}, cancel);
|
||||
|
||||
if (request.Action.ErrorAction == ErrorAction.Stop)
|
||||
throw;
|
||||
}
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
@@ -11,30 +11,30 @@ 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);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
}
|
||||
|
||||
await sender.Send(new InsertResultCommand()
|
||||
{
|
||||
ActionId = request.Action.Id,
|
||||
Info = info,
|
||||
Error = error
|
||||
Info = JsonSerializer.Serialize(result)
|
||||
}, cancel);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await sender.Send(new InsertResultCommand()
|
||||
{
|
||||
ActionId = request.Action.Id,
|
||||
Error = ex.ToString()
|
||||
}, cancel);
|
||||
|
||||
if (error is not null && request.Action.ErrorAction == ErrorAction.Stop)
|
||||
return Unit.Value;
|
||||
if (request.Action.ErrorAction == ErrorAction.Stop)
|
||||
throw;
|
||||
}
|
||||
|
||||
return await next(cancel);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user