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(google_pagespeed_insights): actionable copy for 5xx and rate limits at validation (#74554)

Tom Owers6e27ae0daebd
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/google_pagespeed_insights.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/google_pagespeed_insights.py
index 2706e5e5f489..dc2ec0655b43 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/google_pagespeed_insights.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/google_pagespeed_insights.py
@@ -255,8 +255,14 @@ def validate_credentials(api_key: str, urls_raw: str | None) -> tuple[bool, str
"Invalid API key, or the PageSpeed Insights API is not enabled for your Google Cloud project. "
"Check the key and enable the PageSpeed Insights API, then reconnect."
)
+ if response.status_code == 429:
+ return False, ("The PageSpeed Insights API is rate limiting requests. Wait a few minutes, then try again.")
+ if response.status_code >= 500:
+ return False, (
+ "The PageSpeed Insights API returned a server error. This is usually temporary. Try again in a few minutes."
+ )
- return False, f"The PageSpeed Insights API returned an unexpected status code: {response.status_code}"
+ return False, ("Could not validate against the PageSpeed Insights API. Check your API key and URL, then try again.")
def get_rows(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/tests/test_google_pagespeed_insights.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/tests/test_google_pagespeed_insights.py
index 02eeabc2d871..f0435da9b3ed 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/tests/test_google_pagespeed_insights.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_pagespeed_insights/tests/test_google_pagespeed_insights.py
@@ -278,17 +278,31 @@ class TestFetch:
class TestValidateCredentials:
+ # 418 is an unmapped status that exercises the catch-all branch.
@pytest.mark.parametrize(
"status, expected_valid",
- [(200, True), (400, False), (403, False), (500, False)],
+ [(200, True), (400, False), (403, False), (429, False), (500, False), (503, False), (418, False)],
)
def test_status_mapping(self, status, expected_valid):
with mock.patch(f"{MODULE}.make_tracked_session") as mock_session:
mock_session.return_value.get.return_value = _response(status)
- is_valid, _ = validate_credentials("test-key", "https://posthog.com")
+ is_valid, message = validate_credentials("test-key", "https://posthog.com")
assert is_valid is expected_valid
+ if not expected_valid:
+ # No error branch, including the catch-all, may echo the raw status code.
+ assert message is not None
+ assert str(status) not in message
+
+ def test_server_error_shows_transient_copy(self):
+ with mock.patch(f"{MODULE}.make_tracked_session") as mock_session:
+ mock_session.return_value.get.return_value = _response(503)
+
+ _, message = validate_credentials("test-key", "https://posthog.com")
+
+ assert message is not None
+ assert "temporary" in message
def test_malformed_urls_is_invalid_without_request(self):
with mock.patch(f"{MODULE}.make_tracked_session") as mock_session: