Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Raise NoFeatureFlagFound instead of handle missing flags #7

Merged
merged 2 commits into from Jul 31, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions flask_featureflags/__init__.py
Expand Up @@ -35,6 +35,11 @@ class StopCheckingFeatureFlags(Exception):
pass


class NoFeatureFlagFound(Exception):
""" Raise this when the feature flag does not exist. """
pass


def AppConfigFlagHandler(feature=None):
""" This is the default handler. It checks for feature flags in the current app's configuration.

Expand Down Expand Up @@ -63,11 +68,7 @@ class DevelopmentConfig(Config):
try:
return current_app.config[FEATURE_FLAGS_CONFIG][feature]
except (AttributeError, KeyError):
if current_app.debug and current_app.config.get(RAISE_ERROR_ON_MISSING_FEATURES, False):
raise KeyError(u"No feature flag defined for {feature}".format(feature=feature))
else:
log.info(u"No feature flag defined for {feature}".format(feature=feature))
return False
raise NoFeatureFlagFound()


class FeatureFlag(object):
Expand All @@ -93,7 +94,6 @@ def init_app(self, app):
else:
app.jinja_env.tests[self.JINJA_TEST_NAME] = self.check


if not hasattr(app, 'extensions'):
app.extensions = {}
app.extensions[EXTENSION_NAME] = self
Expand All @@ -119,14 +119,25 @@ def check(self, feature):
The order of handlers matters - we will immediately return True if any handler returns true.

If you want to a handler to return False and stop the chain, raise the StopCheckingFeatureFlags exception."""
found = False
for handler in self.handlers:
try:
if handler(feature):
return True
except StopCheckingFeatureFlags:
return False
else:
return False
except NoFeatureFlagFound:
pass
else:
found = True

if not found:
if current_app.debug and current_app.config.get(RAISE_ERROR_ON_MISSING_FEATURES, False):
raise KeyError(u"No feature flag defined for {feature}".format(feature=feature))
else:
log.info(u"No feature flag defined for {feature}".format(feature=feature))

return False


def is_active(feature):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_edge_cases.py
Expand Up @@ -36,4 +36,5 @@ def test_running_default_handler_on_app_that_was_never_set_up_returns_false(self
test_app = Flask(__name__)

with test_app.test_request_context("/"):
self.assertFalse(feature_flags.AppConfigFlagHandler("BOGUS_FEATURE_FLAG"))
self.assertRaises(feature_flags.NoFeatureFlagFound,
feature_flags.AppConfigFlagHandler, "BOGUS_FEATURE_FLAG")