: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(mailchimp): surface a friendly error when the API is unreachable (#64787)
Tom Owers91d60831eb67
diff --git a/posthog/temporal/data_imports/sources/mailchimp/mailchimp.py b/posthog/temporal/data_imports/sources/mailchimp/mailchimp.pyindex 7e4c3ebf786d..552bb1fbed53 100644--- a/posthog/temporal/data_imports/sources/mailchimp/mailchimp.py+++ b/posthog/temporal/data_imports/sources/mailchimp/mailchimp.py@@ -180,8 +180,8 @@ def validate_credentials(api_key: str) -> tuple[bool, str | None]:passreturn False, response.text- except RequestException as e:- return False, str(e)+ except RequestException:+ return False, "Could not reach the Mailchimp API. Check your network connection and try again."def _fetch_all_lists(api_key: str, dc: str) -> list[dict[str, Any]]:diff --git a/posthog/temporal/data_imports/sources/mailchimp/tests/test_mailchimp.py b/posthog/temporal/data_imports/sources/mailchimp/tests/test_mailchimp.pyindex f3847ac26eef..4ea386c59eaa 100644--- a/posthog/temporal/data_imports/sources/mailchimp/tests/test_mailchimp.py+++ b/posthog/temporal/data_imports/sources/mailchimp/tests/test_mailchimp.py@@ -7,6 +7,7 @@ import pytestfrom unittest.mock import MagicMock, patchfrom requests import Request, Response+from requests.exceptions import ProxyError, RequestExceptionfrom posthog.temporal.data_imports.sources.common.resumable import ResumableSourceManagerfrom posthog.temporal.data_imports.sources.mailchimp.mailchimp import (@@ -17,6 +18,7 @@ from posthog.temporal.data_imports.sources.mailchimp.mailchimp import (_get_contacts_iterator,extract_data_center,mailchimp_source,+ validate_credentials,)@@ -47,6 +49,40 @@ class TestExtractDataCenter:extract_data_center(malicious_key)+class TestValidateCredentials:+ def test_success(self, monkeypatch):+ get_mock = MagicMock(return_value=_make_http_response({}, status_code=200))+ monkeypatch.setattr(+ "posthog.temporal.data_imports.sources.mailchimp.mailchimp.make_tracked_session",+ lambda *a, **k: type("_S", (), {"get": staticmethod(get_mock)})(),+ )+ assert validate_credentials("key-us6") == (True, None)++ def test_invalid_api_key_format_is_surfaced(self):+ valid, error = validate_credentials("invalidkey")+ assert valid is False+ assert error is not None and "Invalid Mailchimp API key format" in error++ @pytest.mark.parametrize(+ "exception",+ [+ ProxyError("Cannot connect to proxy.", OSError("Tunnel connection failed: 502 Bad gateway")),+ RequestException("connection reset"),+ ],+ )+ def test_network_errors_return_friendly_message_without_raw_details(self, monkeypatch, exception):+ get_mock = MagicMock(side_effect=exception)+ monkeypatch.setattr(+ "posthog.temporal.data_imports.sources.mailchimp.mailchimp.make_tracked_session",+ lambda *a, **k: type("_S", (), {"get": staticmethod(get_mock)})(),+ )++ valid, error = validate_credentials("key-us6")++ assert valid is False+ assert error == "Could not reach the Mailchimp API. Check your network connection and try again."++class TestFormatIncrementalValue:def test_datetime(self):dt = datetime(2024, 1, 15, 10, 30, 45)