Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(twitter): explain depleted api credits when connecting an x account (#113734)

Tom Owersf81072e0a1ae
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/twitter/tests/test_twitter.py b/products/warehouse_sources/backend/temporal/data_imports/sources/twitter/tests/test_twitter.py
index e8d3f33404a9..f256f1a11d6d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/twitter/tests/test_twitter.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/twitter/tests/test_twitter.py
@@ -101,7 +101,9 @@ class TestValidateCredentials:
(200, {"data": {"id": "1"}}, True, None),
(401, {"title": "Unauthorized"}, False, "rejected that bearer token"),
(403, {"title": "Forbidden"}, False, "API credits"),
- (500, {}, False, "HTTP 500"),
+ (402, {"title": "CreditsDepleted"}, False, "run out of API credits"),
+ (500, {}, False, "Try again in a few minutes"),
+ (400, {"title": "Invalid Request"}, False, "Check the handle and bearer token"),
# X answers an unknown handle with 200 and an `errors` array, not a 404.
(200, {"errors": [{"title": "Not Found Error"}]}, False, "no account with the handle"),
],
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/twitter/twitter.py b/products/warehouse_sources/backend/temporal/data_imports/sources/twitter/twitter.py
index bb8ea0ea8e86..e2a90a9b9b1f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/twitter/twitter.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/twitter/twitter.py
@@ -123,8 +123,16 @@ def validate_credentials(bearer_token: str, username: str) -> tuple[bool, str |
"That token's X app cannot read user lookups. Check the app is attached to a project, "
"and that the project has API credits, then try again.",
)
+ if response.status_code == 402:
+ # X's pay-per-use plan answers 402 once the project's prepaid credits run out.
+ return (
+ False,
+ "Your X developer project has run out of API credits. Add credits in the X developer portal, then try again.",
+ )
+ if response.status_code == 429 or response.status_code >= 500:
+ return False, "PostHog couldn't reach X to check your handle. Try again in a few minutes."
if response.status_code != 200:
- return False, f"X returned HTTP {response.status_code} for the handle @{handle}."
+ return False, "X couldn't look up that handle. Check the handle and bearer token, then try again."
if not (response.json().get("data") or {}).get("id"):
return False, f"X has no account with the handle @{handle}."