From 28464ae4ee9685df441f37f7cd61031ebb981b85 Mon Sep 17 00:00:00 2001 From: Jimmy Campbell Date: Wed, 1 Sep 2021 11:31:49 -0700 Subject: [PATCH 1/2] Added option to throw for missing features. --- .../FeatureManagementError.cs | 7 +++++- .../FeatureManagementOptions.cs | 6 +++++ .../FeatureManager.cs | 13 ++++++++++ .../FeatureManagement.cs | 25 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.FeatureManagement/FeatureManagementError.cs b/src/Microsoft.FeatureManagement/FeatureManagementError.cs index a1e3319f..1cdb9eed 100644 --- a/src/Microsoft.FeatureManagement/FeatureManagementError.cs +++ b/src/Microsoft.FeatureManagement/FeatureManagementError.cs @@ -16,6 +16,11 @@ public enum FeatureManagementError /// /// A feature filter configured for the feature being evaluated is an ambiguous reference to multiple registered feature filters. /// - AmbiguousFeatureFilter + AmbiguousFeatureFilter, + + /// + /// A feature that was requested for evaluation was not found. + /// + MissingFeature } } diff --git a/src/Microsoft.FeatureManagement/FeatureManagementOptions.cs b/src/Microsoft.FeatureManagement/FeatureManagementOptions.cs index 9322934e..23c2b6c3 100644 --- a/src/Microsoft.FeatureManagement/FeatureManagementOptions.cs +++ b/src/Microsoft.FeatureManagement/FeatureManagementOptions.cs @@ -13,5 +13,11 @@ public class FeatureManagementOptions /// If missing feature filters are not ignored an exception will be thrown when attempting to evaluate a feature that depends on a missing feature filter. /// public bool IgnoreMissingFeatureFilters { get; set; } + + /// + /// Controls the behavior of feature evaluation when the target feature is missing. + /// If missing features are not ignored an exception will be thrown when attempting to evaluate them. + /// + public bool IgnoreMissingFeatures { get; set; } = true; } } diff --git a/src/Microsoft.FeatureManagement/FeatureManager.cs b/src/Microsoft.FeatureManagement/FeatureManager.cs index 25873f50..74bd10ab 100644 --- a/src/Microsoft.FeatureManagement/FeatureManager.cs +++ b/src/Microsoft.FeatureManagement/FeatureManager.cs @@ -141,6 +141,19 @@ private async Task IsEnabledAsync(string feature, TContext appCo } } } + else + { + string errorMessage = $"The feature declaration for the feature '{feature}' was not found."; + + if (!_options.IgnoreMissingFeatures) + { + throw new FeatureManagementException(FeatureManagementError.MissingFeatureFilter, errorMessage); + } + else + { + _logger.LogWarning(errorMessage); + } + } foreach (ISessionManager sessionManager in _sessionManagers) { diff --git a/tests/Tests.FeatureManagement/FeatureManagement.cs b/tests/Tests.FeatureManagement/FeatureManagement.cs index e30888e4..d7e85366 100644 --- a/tests/Tests.FeatureManagement/FeatureManagement.cs +++ b/tests/Tests.FeatureManagement/FeatureManagement.cs @@ -485,6 +485,31 @@ public async Task SwallowsExceptionForMissingFeatureFilter() Assert.False(isEnabled); } + [Fact] + public async Task ThrowsForMissingFeatures() + { + IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); + + var services = new ServiceCollection(); + + services + .Configure(options => + { + options.IgnoreMissingFeatures = false; + }); + + services + .AddSingleton(config) + .AddFeatureManagement(); + + ServiceProvider serviceProvider = services.BuildServiceProvider(); + + IFeatureManager featureManager = serviceProvider.GetRequiredService(); + + FeatureManagementException fme = await Assert.ThrowsAsync(() => + featureManager.IsEnabledAsync("NonExistentFeature")); + } + [Fact] public async Task CustomFeatureDefinitionProvider() { From 485371b1eefcc813cc90e814c871949b81aea10c Mon Sep 17 00:00:00 2001 From: Jimmy Campbell Date: Wed, 1 Sep 2021 15:04:19 -0700 Subject: [PATCH 2/2] Add default value documentation for feature management options. --- src/Microsoft.FeatureManagement/FeatureManagementOptions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Microsoft.FeatureManagement/FeatureManagementOptions.cs b/src/Microsoft.FeatureManagement/FeatureManagementOptions.cs index 23c2b6c3..e425c054 100644 --- a/src/Microsoft.FeatureManagement/FeatureManagementOptions.cs +++ b/src/Microsoft.FeatureManagement/FeatureManagementOptions.cs @@ -11,12 +11,14 @@ public class FeatureManagementOptions /// /// Controls the behavior of feature evaluation when dependent feature filters are missing. /// If missing feature filters are not ignored an exception will be thrown when attempting to evaluate a feature that depends on a missing feature filter. + /// The default value is false. /// public bool IgnoreMissingFeatureFilters { get; set; } /// /// Controls the behavior of feature evaluation when the target feature is missing. /// If missing features are not ignored an exception will be thrown when attempting to evaluate them. + /// The default value is true. /// public bool IgnoreMissingFeatures { get; set; } = true; }