Skip to content

Commit 6534afd

Browse files
EDsCODEneilkakkar
andauthored
fix: change api error to log (#83)
* change api error to log * change implementation * sort * format * raise when debug is true * Update posthog/test/test_feature_flags.py * format --------- Co-authored-by: Neil Kakkar <[email protected]>
1 parent a9d7bf3 commit 6534afd

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

posthog/client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def __init__(
4848
personal_api_key=None,
4949
project_api_key=None,
5050
):
51-
5251
self.queue = queue.Queue(max_queue_size)
5352

5453
# api_key: This should be the Team API Key (token), public
@@ -383,12 +382,16 @@ def _load_feature_flags(self):
383382

384383
except APIError as e:
385384
if e.status == 401:
386-
raise APIError(
387-
status=401,
388-
message="You are using a write-only key with feature flags. "
389-
"To use feature flags, please set a personal_api_key "
390-
"More information: https://posthog.com/docs/api/overview",
385+
self.log.error(
386+
f"[FEATURE FLAGS] Error loading feature flags: To use feature flags, please set a valid personal_api_key. More information: https://posthog.com/docs/api/overview"
391387
)
388+
if self.debug:
389+
raise APIError(
390+
status=401,
391+
message="You are using a write-only key with feature flags. "
392+
"To use feature flags, please set a personal_api_key "
393+
"More information: https://posthog.com/docs/api/overview",
394+
)
392395
else:
393396
self.log.error(f"[FEATURE FLAGS] Error loading feature flags: {e}")
394397
except Exception as e:
@@ -412,7 +415,6 @@ def load_feature_flags(self):
412415
self.poller.start()
413416

414417
def _compute_flag_locally(self, feature_flag, distinct_id, *, groups={}, person_properties={}, group_properties={}):
415-
416418
if feature_flag.get("ensure_experience_continuity", False):
417419
raise InconclusiveMatchError("Flag has experience continuity enabled")
418420

posthog/test/test_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def test_basic_capture_with_uuid(self):
7171
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
7272

7373
def test_basic_capture_with_project_api_key(self):
74-
7574
client = Client(project_api_key=FAKE_TEST_API_KEY, on_error=self.set_fail)
7675

7776
success, msg = client.capture("distinct_id", "python test event")

posthog/test/test_feature_flags.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,8 +981,15 @@ def test_load_feature_flags(self, patch_get, patch_poll):
981981

982982
def test_load_feature_flags_wrong_key(self):
983983
client = Client(FAKE_TEST_API_KEY, personal_api_key=FAKE_TEST_API_KEY)
984-
with freeze_time("2020-01-01T12:01:00.0000Z"):
985-
self.assertRaises(APIError, client.load_feature_flags)
984+
985+
with self.assertLogs("posthog", level="ERROR") as logs:
986+
client.load_feature_flags()
987+
self.assertEqual(
988+
logs.output[0],
989+
"ERROR:posthog:[FEATURE FLAGS] Error loading feature flags: To use feature flags, please set a valid personal_api_key. More information: https://posthog.com/docs/api/overview",
990+
)
991+
client.debug = True
992+
self.assertRaises(APIError, client.load_feature_flags)
986993

987994
@mock.patch("posthog.client.decide")
988995
@mock.patch("posthog.client.get")

0 commit comments

Comments
 (0)