From 08c0d29d84ee646970dd4d97170004f1095512f9 Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 25 Mar 2026 10:25:52 +0100 Subject: [PATCH] Improve error handling and logging in batch rec actions Add ILogger support to InvokeRecActionViewsCommandHandler for enhanced error and warning logging. Log and continue on handled exceptions when appropriate, and rethrow for critical errors. Clean up and reorder using directives. This increases robustness and traceability during batch rec action processing. --- .../InvokeBatchRecActionViewsCommand.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionViewsCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionViewsCommand.cs index b30a8bb..e134c5d 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionViewsCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeBatchRecActionViewsCommand.cs @@ -1,5 +1,6 @@ -using DigitalData.Core.Abstractions.Interfaces; -using MediatR; +using MediatR; +using Microsoft.Extensions.Logging; +using ReC.Application.Common.Exceptions; using ReC.Application.RecActions.Queries; using ReC.Domain.Constants; @@ -10,7 +11,7 @@ public record InvokeBatchRecActionViewsCommand : IRequest public long ProfileId { get; init; } } -public class InvokeRecActionViewsCommandHandler(ISender sender) : IRequestHandler +public class InvokeRecActionViewsCommandHandler(ISender sender, ILogger? logger = null) : IRequestHandler { public async Task Handle(InvokeBatchRecActionViewsCommand request, CancellationToken cancel) { @@ -22,11 +23,24 @@ public class InvokeRecActionViewsCommandHandler(ISender sender) : IRequestHandle { await sender.Send(new InvokeRecActionViewCommand() { Action = action }, cancel); } - catch + catch (RecActionException ex) { switch (action.ErrorAction) { case ErrorAction.Continue: + logger?.LogWarning(ex, "Rec action failed but continuing. ActionId: {ActionId}, ProfileId: {ProfileId}", ex.ActionId, ex.ProfileId); + break; + default: + // Rethrow the exception to stop processing further actions + throw; + } + } + catch (Exception ex) + { + switch (action.ErrorAction) + { + case ErrorAction.Continue: + logger?.LogError(ex, "Unexpected error during rec action. ActionId: {ActionId}, ProfileId: {ProfileId}", action.Id, action.ProfileId); break; default: // Rethrow the exception to stop processing further actions