: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(revenuecat): drop http status codes from source setup errors (#99818)
Tom Owers4d9f07ab7a15
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/revenuecat.py b/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/revenuecat.pyindex 91b93287f5ee..57512de0393b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/revenuecat.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/revenuecat.py@@ -159,9 +159,9 @@ def _project_not_found_error(entered_project_id: str, accessible_ids: list[str])"""Turn an opaque 404 into an actionable message naming the reachable project ids."""if not accessible_ids:return (- f"RevenueCat could not find the project '{entered_project_id}' (404), and this API key "- "can't see any projects. Generate a v2 secret API key in the RevenueCat project you want "- "to sync, then re-enter its Project ID."+ f"RevenueCat could not find the project '{entered_project_id}', and this API key "+ "can't see any projects. Generate a v2 secret API key in the RevenueCat project you "+ "want to sync, then re-enter its Project ID.")if len(accessible_ids) == 1:only = accessible_ids[0]@@ -179,24 +179,30 @@ def _project_not_found_error(entered_project_id: str, accessible_ids: list[str]))+# A transport failure leaves the key unjudged, so it must not read as a credential problem.+UNREACHABLE_MESSAGE = "PostHog couldn't reach RevenueCat. Wait a few minutes and try again."++def _format_http_error(error: requests.HTTPError, forbidden_hint: str | None = None) -> str:response = error.responsestatus_code = response.status_code if response is not None else Noneif status_code == 401:return (- "RevenueCat rejected the API key (401). Generate a v2 secret API key "- "from Project settings > API keys and check that it has not been revoked."+ "RevenueCat rejected your API key. Generate a new v2 secret API key under "+ "Project settings > API keys, then reconnect.")if status_code == 403:# This formatter also serves webhook create/delete, which need write scope, so the default# stays scope-neutral. Read-only callers pass a read-specific hint.- detail = forbidden_hint or "Make sure the v2 secret API key has the permissions this request needs."- return f"RevenueCat denied the request (403). {detail}"+ detail = forbidden_hint or "Give your v2 secret API key the permissions this request needs, then reconnect."+ return f"RevenueCat denied the request. {detail}"if status_code == 404:- return "RevenueCat could not find the project (404). Double-check the project id."+ return (+ "RevenueCat could not find the project. Check the project id in your RevenueCat dashboard, then reconnect."+ )if status_code == 429:- return "RevenueCat rate-limited the request (429). Try again in a few seconds."- return f"RevenueCat API error ({status_code})."+ return "RevenueCat rate-limited the request. Try again in a few seconds."+ return "RevenueCat could not complete the request. Try again in a few minutes, and check the RevenueCat status page if it keeps failing."def validate_credentials(api_key: str, project_id: str | None) -> tuple[bool, str | None]:@@ -218,12 +224,12 @@ def validate_credentials(api_key: str, project_id: str | None) -> tuple[bool, streturn False, _format_http_error(e,forbidden_hint=(- "The v2 secret API key is missing read access. Give it read permission for the data "- "you want to sync, then reconnect."+ "Give your v2 secret API key read permission for the data you want to sync, then reconnect."),)except requests.RequestException as e:- return False, f"Could not reach RevenueCat: {e}"+ LOGGER.warning("Could not reach RevenueCat to validate credentials", error=str(e))+ return False, UNREACHABLE_MESSAGEnormalized_project_id = _normalize_project_id(project_id)if not normalized_project_id:@@ -477,14 +483,13 @@ def create_webhook(# The read-only hint the validate path passes doesn't fit here: registering the# integration needs write scope, and the failure screen offers manual setup.forbidden_hint=(- "The v2 secret API key is missing write access. Give it write permission for "- "integrations, or follow the manual setup below."+ "Give your v2 secret API key write permission for integrations, or follow the manual setup below."),),)except requests.RequestException as e:logger.warning("Could not reach RevenueCat to register webhook", error=str(e))- return WebhookCreationResult(success=False, error=f"Could not reach RevenueCat: {e}")+ return WebhookCreationResult(success=False, error=UNREACHABLE_MESSAGE)pending: list[str] = []if not authorization_header_value:@@ -542,7 +547,7 @@ def delete_webhook(api_key: str, project_id: str, webhook_url: str) -> WebhookDereturn WebhookDeletionResult(success=False, error=_format_http_error(e))except requests.RequestException as e:logger.warning("Could not reach RevenueCat to list webhooks", error=str(e))- return WebhookDeletionResult(success=False, error=f"Could not reach RevenueCat: {e}")+ return WebhookDeletionResult(success=False, error=UNREACHABLE_MESSAGE)target = next((hook for hook in integrations if hook.get("url") == webhook_url), None)if target is None:@@ -567,7 +572,7 @@ def delete_webhook(api_key: str, project_id: str, webhook_url: str) -> WebhookDereturn WebhookDeletionResult(success=False, error=_format_http_error(e))except requests.RequestException as e:logger.warning("Could not reach RevenueCat to delete webhook", error=str(e))- return WebhookDeletionResult(success=False, error=f"Could not reach RevenueCat: {e}")+ return WebhookDeletionResult(success=False, error=UNREACHABLE_MESSAGE)return WebhookDeletionResult(success=True)@@ -579,7 +584,8 @@ def get_external_webhook_info(api_key: str, project_id: str, webhook_url: str) -except requests.HTTPError as e:return ExternalWebhookInfo(exists=False, error=_format_http_error(e))except requests.RequestException as e:- return ExternalWebhookInfo(exists=False, error=f"Could not reach RevenueCat: {e}")+ LOGGER.warning("Could not reach RevenueCat to look up the webhook", error=str(e))+ return ExternalWebhookInfo(exists=False, error=UNREACHABLE_MESSAGE)target = next((hook for hook in integrations if hook.get("url") == webhook_url), None)if target is None:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/tests/test_revenuecat.py b/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/tests/test_revenuecat.pyindex 854f26de9f2a..bc886b998e48 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/tests/test_revenuecat.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/tests/test_revenuecat.py@@ -157,11 +157,11 @@ class TestValidateCredentials:@parameterized.expand([- (401, "rejected the API key"),+ (401, "rejected your api key"),(403, "denied"),(404, "could not find"),(429, "rate-limited"),- (500, "RevenueCat API error"),+ (500, "could not complete"),])@patch("products.warehouse_sources.backend.temporal.data_imports.sources.revenuecat.revenuecat._session")@@ -173,6 +173,8 @@ class TestValidateCredentials:assert success is Falseassert error is not Noneassert expected_substring.lower() in error.lower()+ # The status code is for error tracking, not for the person reading the source setup form.+ assert str(status_code) not in error@patch("products.warehouse_sources.backend.temporal.data_imports.sources.revenuecat.revenuecat._session")def test_returns_false_on_network_error(self, mock_session):@@ -182,7 +184,9 @@ class TestValidateCredentials:assert success is Falseassert error is not None- assert "Could not reach RevenueCat" in error+ assert error == api_client.UNREACHABLE_MESSAGE+ # The transport exception must not reach the form — it names hosts and internals.+ assert "dns fail" not in error@patch("products.warehouse_sources.backend.temporal.data_imports.sources.revenuecat.revenuecat._session")def test_skips_project_check_when_id_normalizes_to_empty(self, mock_session):