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

Commit

Permalink
Merge pull request #7 from iurisilvio/raise_notfound
Browse files Browse the repository at this point in the history
Raise NoFeatureFlagFound instead of handle missing flags
  • Loading branch information
Rachel Sanders committed Jul 31, 2014
2 parents 06641d1 + 8a42958 commit 2ddb4c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
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")

0 comments on commit 2ddb4c4

Please sign in to comment.