From a707cce6e46998f6991e1666a8119fc7d683f9a0 Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 24 Mar 2026 16:19:00 +0100 Subject: [PATCH] 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. --- .../Action/PostprocessingBehavior.cs | 38 +++++++++---------- .../Behaviors/Action/PreprocessingBehavior.cs | 28 +++++++------- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs b/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs index 222081b..a854c16 100644 --- a/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs +++ b/src/ReC.Application/Common/Behaviors/Action/PostprocessingBehavior.cs @@ -11,37 +11,35 @@ public class PostprocessingBehavior(IRecDbContext context, ISender sender) : IPi { public async Task Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate 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; } diff --git a/src/ReC.Application/Common/Behaviors/Action/PreprocessingBehavior.cs b/src/ReC.Application/Common/Behaviors/Action/PreprocessingBehavior.cs index 7383c17..9a3009b 100644 --- a/src/ReC.Application/Common/Behaviors/Action/PreprocessingBehavior.cs +++ b/src/ReC.Application/Common/Behaviors/Action/PreprocessingBehavior.cs @@ -11,30 +11,30 @@ public class PreprocessingBehavior(IRecDbContext context, ISender sender) : IPip { public async Task Handle(InvokeRecActionViewCommand request, RequestHandlerDelegate 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, - Info = info, - Error = error - }, cancel); + 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); }