Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/feature-flag-auth-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Improve local feature flag authentication error messages.
14 changes: 6 additions & 8 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,9 +1353,12 @@ def _fetch_feature_flags_from_api(self):

except APIError as e:
if e.status == 401:
self.log.error(
"[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"
detail = (
f"Error loading feature flags: {e.message}. "
"Please verify both your project_api_key and personal_api_key. "
"More information: https://posthog.com/docs/api/overview"
)
self.log.error("[FEATURE FLAGS] %s", detail)
self.feature_flags = []
self.group_type_mapping = {}
self.cohorts = {}
Expand All @@ -1364,12 +1367,7 @@ def _fetch_feature_flags_from_api(self):
self.flag_cache.clear()

if self.debug:
raise APIError(
status=401,
message="You are using a write-only key with feature flags. "
"To use feature flags, please set a personal_api_key "
"More information: https://posthog.com/docs/api/overview",
)
raise APIError(status=401, message=detail)
elif e.status == 402:
self.log.warning(
"[FEATURE FLAGS] PostHog feature flags quota limited, resetting feature flag data. Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts"
Expand Down
4 changes: 3 additions & 1 deletion posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,9 @@ def test_load_feature_flags_unauthorized(self, patch_get):
self.assertEqual(client.feature_flags_by_key, {})
self.assertEqual(client.group_type_mapping, {})
self.assertEqual(client.cohorts, {})
self.assertIn("please set a valid personal_api_key", logs.output[0])
self.assertIn("Unauthorized", logs.output[0])
self.assertIn("project_api_key", logs.output[0])
self.assertIn("personal_api_key", logs.output[0])

@mock.patch("posthog.client.flags")
def test_dont_override_capture_with_local_flags(self, patch_flags):
Expand Down
10 changes: 5 additions & 5 deletions posthog/test/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2546,12 +2546,12 @@ def test_load_feature_flags_wrong_key(self, patch_get, _patch_poll):

with self.assertLogs("posthog", level="ERROR") as logs:
client.load_feature_flags()
self.assertEqual(
logs.output[0],
"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",
)
self.assertIn("Unauthorized", logs.output[0])
self.assertIn("project_api_key", logs.output[0])
self.assertIn("personal_api_key", logs.output[0])
client.debug = True
self.assertRaises(APIError, client.load_feature_flags)
with self.assertRaisesRegex(APIError, "Unauthorized"):
client.load_feature_flags()

@mock.patch("posthog.client.flags")
@mock.patch("posthog.client.get")
Expand Down
Loading