Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to throw when attempting to evaluate a missing feature. #140

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Microsoft.FeatureManagement/FeatureManagementError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public enum FeatureManagementError
/// <summary>
/// A feature filter configured for the feature being evaluated is an ambiguous reference to multiple registered feature filters.
/// </summary>
AmbiguousFeatureFilter
AmbiguousFeatureFilter,

/// <summary>
/// A feature that was requested for evaluation was not found.
/// </summary>
MissingFeature
}
}
8 changes: 8 additions & 0 deletions src/Microsoft.FeatureManagement/FeatureManagementOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ public class FeatureManagementOptions
/// <summary>
/// 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.
/// </summary>
public bool IgnoreMissingFeatureFilters { get; set; }

/// <summary>
/// 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.
/// </summary>
jimmyca15 marked this conversation as resolved.
Show resolved Hide resolved
public bool IgnoreMissingFeatures { get; set; } = true;
}
}
13 changes: 13 additions & 0 deletions src/Microsoft.FeatureManagement/FeatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ private async Task<bool> IsEnabledAsync<TContext>(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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this error type be FeatureManagementError.MissingFeature?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks. Here's a PR to fix: #142

}
else
{
_logger.LogWarning(errorMessage);
}
}

foreach (ISessionManager sessionManager in _sessionManagers)
{
Expand Down
25 changes: 25 additions & 0 deletions tests/Tests.FeatureManagement/FeatureManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FeatureManagementOptions>(options =>
{
options.IgnoreMissingFeatures = false;
});

services
.AddSingleton(config)
.AddFeatureManagement();

ServiceProvider serviceProvider = services.BuildServiceProvider();

IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>();

FeatureManagementException fme = await Assert.ThrowsAsync<FeatureManagementException>(() =>
featureManager.IsEnabledAsync("NonExistentFeature"));
}

[Fact]
public async Task CustomFeatureDefinitionProvider()
{
Expand Down