From 96a5e5a07b8d4b4aa55d31bcab14219ee6912a04 Mon Sep 17 00:00:00 2001 From: Amer Jusupovic <32405726+amerjusupovic@users.noreply.github.com> Date: Fri, 21 Jun 2024 12:40:17 -0700 Subject: [PATCH] Add handling for FormatException thrown by invalid feature flag json (#551) * add log for formatexception from invalid feature flag json * remove capitalized json * remove unused using * fix comments * fix constant reference --- .../AzureAppConfigurationProvider.cs | 7 +++++++ .../Constants/LoggingConstants.cs | 1 + .../FeatureManagement/FeatureManagementKeyValueAdapter.cs | 2 +- .../LogHelper.cs | 5 +++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationProvider.cs b/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationProvider.cs index 9cf9947b..182c819a 100644 --- a/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationProvider.cs +++ b/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationProvider.cs @@ -492,6 +492,12 @@ public async Task TryRefreshAsync(CancellationToken cancellationToken) return false; } + catch (FormatException fe) + { + _logger.LogWarning(LogHelper.BuildRefreshFailedDueToFormattingErrorMessage(fe.Message)); + + return false; + } return true; } @@ -634,6 +640,7 @@ exception is KeyVaultReferenceException || exception is TimeoutException || exception is OperationCanceledException || exception is InvalidOperationException || + exception is FormatException || ((exception as AggregateException)?.InnerExceptions?.Any(e => e is RequestFailedException || e is OperationCanceledException) ?? false))) diff --git a/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/Constants/LoggingConstants.cs b/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/Constants/LoggingConstants.cs index 41c56ca3..86576a48 100644 --- a/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/Constants/LoggingConstants.cs +++ b/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/Constants/LoggingConstants.cs @@ -15,6 +15,7 @@ internal class LoggingConstants public const string RefreshFailedDueToKeyVaultError = "A refresh operation failed while resolving a Key Vault reference."; public const string PushNotificationUnregisteredEndpoint = "Ignoring the push notification received for the unregistered endpoint"; public const string FallbackClientLookupError = "Failed to perform fallback client lookup."; + public const string RefreshFailedDueToFormattingError = "A refresh operation failed due to a formatting error."; // Successful update, debug log level public const string RefreshKeyValueRead = "Key-value read from App Configuration."; diff --git a/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/FeatureManagement/FeatureManagementKeyValueAdapter.cs b/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/FeatureManagement/FeatureManagementKeyValueAdapter.cs index a4f64d6b..5b0a6a96 100644 --- a/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/FeatureManagement/FeatureManagementKeyValueAdapter.cs +++ b/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/FeatureManagement/FeatureManagementKeyValueAdapter.cs @@ -188,7 +188,7 @@ private FeatureFlag ParseFeatureFlag(string settingKey, string settingValue) } catch (JsonException e) { - throw new FormatException(settingKey, e); + throw new FormatException(string.Format(ErrorMessages.FeatureFlagInvalidFormat, settingKey), e); } return featureFlag; diff --git a/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/LogHelper.cs b/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/LogHelper.cs index 113a1499..e7429a9e 100644 --- a/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/LogHelper.cs +++ b/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/LogHelper.cs @@ -89,5 +89,10 @@ public static string BuildFallbackClientLookupFailMessage(string exceptionMessag { return $"{LoggingConstants.FallbackClientLookupError}\n{exceptionMessage}"; } + + public static string BuildRefreshFailedDueToFormattingErrorMessage(string exceptionMessage) + { + return $"{LoggingConstants.RefreshFailedDueToFormattingError}\n{exceptionMessage}"; + } } }