Skip to content

fix: clarify feature flag auth errors#542

Open
marandaneto wants to merge 1 commit intoPostHog:mainfrom
marandaneto:fix/feature-flag-auth-error-message
Open

fix: clarify feature flag auth errors#542
marandaneto wants to merge 1 commit intoPostHog:mainfrom
marandaneto:fix/feature-flag-auth-error-message

Conversation

@marandaneto
Copy link
Copy Markdown
Member

@marandaneto marandaneto commented Apr 30, 2026

💡 Motivation and Context

Relates to #526.

Local feature flag loading uses both the project API key and the personal/feature-flags API key, but 401 errors always blamed only personal_api_key. This keeps the server-provided error detail and asks users to verify both credentials.

💚 How did you test it?

  • uv run pytest posthog/test/test_client.py::TestClient::test_load_feature_flags_unauthorized posthog/test/test_feature_flags.py::TestLocalEvaluation::test_load_feature_flags_wrong_key -q
  • uv run ruff format --check posthog/client.py posthog/test/test_client.py posthog/test/test_feature_flags.py
  • uv run ruff check posthog/client.py posthog/test/test_client.py posthog/test/test_feature_flags.py

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file
  • Added the release label to the PR

@marandaneto marandaneto marked this pull request as ready for review April 30, 2026 06:02
@marandaneto marandaneto requested a review from a team as a code owner April 30, 2026 06:02
@marandaneto marandaneto requested a review from haacked April 30, 2026 06:02
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 30, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
posthog/client.py:1353-1370
**Duplicated message content (OnceAndOnlyOnce)**

The error message body — `"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"` — is constructed twice: once as a `%s` format string for the log call, and again as an f-string for the re-raised `APIError`. Extracting it into a local variable eliminates the duplication and keeps the two outputs in sync automatically.

```suggestion
                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 = {}

                if self.flag_cache:
                    self.flag_cache.clear()

                if self.debug:
                    raise APIError(status=401, message=detail)
```

Reviews (1): Last reviewed commit: "fix: clarify feature flag auth errors" | Re-trigger Greptile

Comment thread posthog/client.py
Comment on lines 1353 to 1370
@@ -1363,8 +1364,8 @@ def _fetch_feature_flags_from_api(self):
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 "
message=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",
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Duplicated message content (OnceAndOnlyOnce)

The error message body — "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" — is constructed twice: once as a %s format string for the log call, and again as an f-string for the re-raised APIError. Extracting it into a local variable eliminates the duplication and keeps the two outputs in sync automatically.

Suggested change
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 = {}
if self.flag_cache:
self.flag_cache.clear()
if self.debug:
raise APIError(status=401, message=detail)
Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/client.py
Line: 1353-1370

Comment:
**Duplicated message content (OnceAndOnlyOnce)**

The error message body — `"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"` — is constructed twice: once as a `%s` format string for the log call, and again as an f-string for the re-raised `APIError`. Extracting it into a local variable eliminates the duplication and keeps the two outputs in sync automatically.

```suggestion
                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 = {}

                if self.flag_cache:
                    self.flag_cache.clear()

                if self.debug:
                    raise APIError(status=401, message=detail)
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown

@ioannisj ioannisj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG, minor optimization from greptile makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants