From 85a961b34588faf22ac655f5fe14b5a0c31820a6 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 30 Apr 2026 10:50:49 +0200 Subject: [PATCH] fix: clarify feature flag auth errors --- .sampo/changesets/feature-flag-auth-errors.md | 5 +++++ posthog/client.py | 14 ++++++-------- posthog/test/test_client.py | 4 +++- posthog/test/test_feature_flags.py | 10 +++++----- 4 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 .sampo/changesets/feature-flag-auth-errors.md diff --git a/.sampo/changesets/feature-flag-auth-errors.md b/.sampo/changesets/feature-flag-auth-errors.md new file mode 100644 index 00000000..e40d03b8 --- /dev/null +++ b/.sampo/changesets/feature-flag-auth-errors.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Improve local feature flag authentication error messages. diff --git a/posthog/client.py b/posthog/client.py index 998f88d1..ac765555 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -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 = {} @@ -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" diff --git a/posthog/test/test_client.py b/posthog/test/test_client.py index db0e6b1a..5275eb6c 100644 --- a/posthog/test/test_client.py +++ b/posthog/test/test_client.py @@ -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): diff --git a/posthog/test/test_feature_flags.py b/posthog/test/test_feature_flags.py index 0ce5e72c..2170ce16 100644 --- a/posthog/test/test_feature_flags.py +++ b/posthog/test/test_feature_flags.py @@ -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")