Skip to content

Commit

Permalink
fix: Add a custom exception for invalid features (#86)
Browse files Browse the repository at this point in the history
* fix: Add a custom exception for invalid features

* feat: add mock success response
  • Loading branch information
tushar5526 authored Jul 16, 2024
1 parent c4d085c commit fd20941
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
4 changes: 4 additions & 0 deletions flagsmith/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ class FlagsmithClientError(Exception):

class FlagsmithAPIError(FlagsmithClientError):
pass


class FlagsmithFeatureDoesNotExistError(FlagsmithClientError):
pass
6 changes: 4 additions & 2 deletions flagsmith/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flag_engine.features.models import FeatureStateModel

from flagsmith.analytics import AnalyticsProcessor
from flagsmith.exceptions import FlagsmithClientError
from flagsmith.exceptions import FlagsmithFeatureDoesNotExistError


@dataclass
Expand Down Expand Up @@ -135,7 +135,9 @@ def get_flag(self, feature_name: str) -> typing.Union[DefaultFlag, Flag]:
except KeyError:
if self.default_flag_handler:
return self.default_flag_handler(feature_name)
raise FlagsmithClientError("Feature does not exist: %s" % feature_name)
raise FlagsmithFeatureDoesNotExistError(
"Feature does not exist: %s" % feature_name
)

if self._analytics_processor and hasattr(flag, "feature_name"):
self._analytics_processor.track_feature(flag.feature_name)
Expand Down
26 changes: 22 additions & 4 deletions tests/test_flagsmith.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
from pytest_mock import MockerFixture

from flagsmith import Flagsmith
from flagsmith.exceptions import FlagsmithAPIError
from flagsmith.exceptions import (
FlagsmithAPIError,
FlagsmithFeatureDoesNotExistError,
)
from flagsmith.models import DefaultFlag, Flags
from flagsmith.offline_handlers import BaseOfflineHandler


def test_flagsmith_starts_polling_manager_on_init_if_enabled(
mocker: MockerFixture,
server_api_key: str,
requests_session_response_ok: None,
mocker: MockerFixture, server_api_key: str, requests_session_response_ok: None
) -> None:
# Given
mock_polling_manager = mocker.MagicMock()
Expand Down Expand Up @@ -559,3 +560,20 @@ def test_flagsmith_client_get_identity_flags__local_evaluation__returns_expected
# Then
assert flag.enabled is False
assert flag.value == "some-overridden-value"


def test_custom_feature_error_raised_when_invalid_feature(
requests_session_response_ok: None, server_api_key: str
) -> None:
# Given
flagsmith = Flagsmith(
environment_key=server_api_key,
enable_local_evaluation=True,
)

flags = flagsmith.get_environment_flags()

# Then
with pytest.raises(FlagsmithFeatureDoesNotExistError):
# When
flags.is_feature_enabled("non-existing-feature")

0 comments on commit fd20941

Please sign in to comment.