Skip to content

Commit 65a4551

Browse files
authored
Added option to throw when attempting to evaluate a missing feature. (#140)
* Added option to throw for missing features. * Add default value documentation for feature management options.
1 parent e531863 commit 65a4551

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/Microsoft.FeatureManagement/FeatureManagementError.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public enum FeatureManagementError
1616
/// <summary>
1717
/// A feature filter configured for the feature being evaluated is an ambiguous reference to multiple registered feature filters.
1818
/// </summary>
19-
AmbiguousFeatureFilter
19+
AmbiguousFeatureFilter,
20+
21+
/// <summary>
22+
/// A feature that was requested for evaluation was not found.
23+
/// </summary>
24+
MissingFeature
2025
}
2126
}

src/Microsoft.FeatureManagement/FeatureManagementOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ public class FeatureManagementOptions
1111
/// <summary>
1212
/// Controls the behavior of feature evaluation when dependent feature filters are missing.
1313
/// 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.
14+
/// The default value is false.
1415
/// </summary>
1516
public bool IgnoreMissingFeatureFilters { get; set; }
17+
18+
/// <summary>
19+
/// Controls the behavior of feature evaluation when the target feature is missing.
20+
/// If missing features are not ignored an exception will be thrown when attempting to evaluate them.
21+
/// The default value is true.
22+
/// </summary>
23+
public bool IgnoreMissingFeatures { get; set; } = true;
1624
}
1725
}

src/Microsoft.FeatureManagement/FeatureManager.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ private async Task<bool> IsEnabledAsync<TContext>(string feature, TContext appCo
141141
}
142142
}
143143
}
144+
else
145+
{
146+
string errorMessage = $"The feature declaration for the feature '{feature}' was not found.";
147+
148+
if (!_options.IgnoreMissingFeatures)
149+
{
150+
throw new FeatureManagementException(FeatureManagementError.MissingFeatureFilter, errorMessage);
151+
}
152+
else
153+
{
154+
_logger.LogWarning(errorMessage);
155+
}
156+
}
144157

145158
foreach (ISessionManager sessionManager in _sessionManagers)
146159
{

tests/Tests.FeatureManagement/FeatureManagement.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,31 @@ public async Task SwallowsExceptionForMissingFeatureFilter()
485485
Assert.False(isEnabled);
486486
}
487487

488+
[Fact]
489+
public async Task ThrowsForMissingFeatures()
490+
{
491+
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
492+
493+
var services = new ServiceCollection();
494+
495+
services
496+
.Configure<FeatureManagementOptions>(options =>
497+
{
498+
options.IgnoreMissingFeatures = false;
499+
});
500+
501+
services
502+
.AddSingleton(config)
503+
.AddFeatureManagement();
504+
505+
ServiceProvider serviceProvider = services.BuildServiceProvider();
506+
507+
IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>();
508+
509+
FeatureManagementException fme = await Assert.ThrowsAsync<FeatureManagementException>(() =>
510+
featureManager.IsEnabledAsync("NonExistentFeature"));
511+
}
512+
488513
[Fact]
489514
public async Task CustomFeatureDefinitionProvider()
490515
{

0 commit comments

Comments
 (0)