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