From a1374f2e6a428278b89d1066e94c594a6a9f1793 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Thu, 27 Feb 2025 16:23:57 +0000 Subject: [PATCH] add error message based on result properties --- .../Common/PolicyFactory.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/TransactionProcessor.BusinessLogic/Common/PolicyFactory.cs b/TransactionProcessor.BusinessLogic/Common/PolicyFactory.cs index faeedccc..ac1ddf00 100644 --- a/TransactionProcessor.BusinessLogic/Common/PolicyFactory.cs +++ b/TransactionProcessor.BusinessLogic/Common/PolicyFactory.cs @@ -1,6 +1,4 @@ -using System; -using System.Threading.Tasks; -using EventStore.Client; +using EventStore.Client; using Grpc.Core; using Polly; using Polly.Fallback; @@ -8,6 +6,9 @@ using Polly.Wrap; using Shared.Logger; using SimpleResults; +using System; +using System.Linq; +using System.Threading.Tasks; namespace TransactionProcessor.BusinessLogic.Common; @@ -29,10 +30,17 @@ public static async Task ExecuteWithPolicyAsync(Func> actio { var context = new Context(); context["RetryCount"] = 0; - //Result result = await policy.ExecuteAsync(action); + Result result = await policy.ExecuteAsync((ctx) => action(), context); + int retryCount = (int)context["RetryCount"]; - String message = result.IsSuccess ? "Execution succeeded." : $"Execution failed with error: {result.Message}"; + String message = result switch + { + { IsSuccess: true } => "Success", + { IsSuccess: false, Message: not "" } => result.Message, + { IsSuccess: false, Message: "", Errors: var errors } when errors.Any() => string.Join(", ", errors), + _ => "Unknown Error" + }; String retryMessage = retryCount > 0 ? $" after {retryCount} retries." : ""; // Log success if no retries were required