Skip to content

Commit fd20941

Browse files
authored
fix: Add a custom exception for invalid features (#86)
* fix: Add a custom exception for invalid features * feat: add mock success response
1 parent c4d085c commit fd20941

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

flagsmith/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ class FlagsmithClientError(Exception):
44

55
class FlagsmithAPIError(FlagsmithClientError):
66
pass
7+
8+
9+
class FlagsmithFeatureDoesNotExistError(FlagsmithClientError):
10+
pass

flagsmith/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from flag_engine.features.models import FeatureStateModel
77

88
from flagsmith.analytics import AnalyticsProcessor
9-
from flagsmith.exceptions import FlagsmithClientError
9+
from flagsmith.exceptions import FlagsmithFeatureDoesNotExistError
1010

1111

1212
@dataclass
@@ -135,7 +135,9 @@ def get_flag(self, feature_name: str) -> typing.Union[DefaultFlag, Flag]:
135135
except KeyError:
136136
if self.default_flag_handler:
137137
return self.default_flag_handler(feature_name)
138-
raise FlagsmithClientError("Feature does not exist: %s" % feature_name)
138+
raise FlagsmithFeatureDoesNotExistError(
139+
"Feature does not exist: %s" % feature_name
140+
)
139141

140142
if self._analytics_processor and hasattr(flag, "feature_name"):
141143
self._analytics_processor.track_feature(flag.feature_name)

tests/test_flagsmith.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
from pytest_mock import MockerFixture
1212

1313
from flagsmith import Flagsmith
14-
from flagsmith.exceptions import FlagsmithAPIError
14+
from flagsmith.exceptions import (
15+
FlagsmithAPIError,
16+
FlagsmithFeatureDoesNotExistError,
17+
)
1518
from flagsmith.models import DefaultFlag, Flags
1619
from flagsmith.offline_handlers import BaseOfflineHandler
1720

1821

1922
def test_flagsmith_starts_polling_manager_on_init_if_enabled(
20-
mocker: MockerFixture,
21-
server_api_key: str,
22-
requests_session_response_ok: None,
23+
mocker: MockerFixture, server_api_key: str, requests_session_response_ok: None
2324
) -> None:
2425
# Given
2526
mock_polling_manager = mocker.MagicMock()
@@ -559,3 +560,20 @@ def test_flagsmith_client_get_identity_flags__local_evaluation__returns_expected
559560
# Then
560561
assert flag.enabled is False
561562
assert flag.value == "some-overridden-value"
563+
564+
565+
def test_custom_feature_error_raised_when_invalid_feature(
566+
requests_session_response_ok: None, server_api_key: str
567+
) -> None:
568+
# Given
569+
flagsmith = Flagsmith(
570+
environment_key=server_api_key,
571+
enable_local_evaluation=True,
572+
)
573+
574+
flags = flagsmith.get_environment_flags()
575+
576+
# Then
577+
with pytest.raises(FlagsmithFeatureDoesNotExistError):
578+
# When
579+
flags.is_feature_enabled("non-existing-feature")

0 commit comments

Comments
 (0)