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(data-imports): catch semrush server errors during credential validation (#112149)

Tom Owers584d4128c069
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/semrush.py b/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/semrush.py
index c6a6fe004741..fbda341873a2 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/semrush.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/semrush.py
@@ -1,10 +1,15 @@
import re
+from requests import RequestException
+
from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source import (
RESTAPIConfig,
rest_api_resource,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.resource import Resource
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client import (
+ RESTClientRetryableError,
+)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.typing import ResponseAction
from products.warehouse_sources.backend.temporal.data_imports.sources.common.schema import schema_for_resource
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceResponse
@@ -17,6 +22,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.semrush.se
MAX_REQUEST_ATTEMPTS,
PROJECT_ID_ERROR,
REQUEST_ERROR,
+ UNAVAILABLE_ERROR,
)
@@ -85,6 +91,8 @@ def validate_credentials(api_key: str, project_id: str, team_id: int) -> tuple[b
if message in {*ERROR_MESSAGES.values(), PROJECT_ID_ERROR, REQUEST_ERROR}:
return False, message
raise
+ except (RequestException, RESTClientRetryableError):
+ return False, UNAVAILABLE_ERROR
return True, None
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/settings.py
index 25c42f23e453..3c829cb6e9ab 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/settings.py
@@ -24,6 +24,7 @@ QUOTA_ERROR = "Semrush API units or request limits are exhausted. Add API units
PROJECT_ERROR = "Semrush could not find this project. Check the project ID and your access to it."
PROJECT_ID_ERROR = "Enter the numeric project ID from your Semrush project URL."
REQUEST_ERROR = "Semrush rejected the request. Check your project ID, Site Audit setup, and API access."
+UNAVAILABLE_ERROR = "Semrush is unavailable or rate-limiting requests. Try again later."
ERROR_MESSAGES = {
70: AUTH_ERROR,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/tests/test_semrush.py b/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/tests/test_semrush.py
index fd4cf1cb5470..2f9e6f47e108 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/tests/test_semrush.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/semrush/tests/test_semrush.py
@@ -6,11 +6,8 @@ from urllib.parse import parse_qs, urlsplit
import pytest
from unittest.mock import patch
-from requests import Response, Session
+from requests import RequestException, Response, Session
-from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client import (
- RESTClientRetryableError,
-)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.schema import UnknownResourceError
from products.warehouse_sources.backend.temporal.data_imports.sources.semrush.semrush import (
semrush_source,
@@ -23,6 +20,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.semrush.se
PROJECT_ID_ERROR,
QUOTA_ERROR,
REQUEST_ERROR,
+ UNAVAILABLE_ERROR,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.semrush.source import SemrushSource
@@ -118,11 +116,15 @@ def test_transient_errors_retry(status: int, body: object) -> None:
def test_retry_limit_bounds_paid_requests() -> None:
with patch.object(Session, "send", return_value=response({}, 503)) as send, patch("tenacity.nap.time.sleep"):
- with pytest.raises(RESTClientRetryableError):
- validate_credentials("fake-api-key", "123", 1)
+ assert validate_credentials("fake-api-key", "123", 1) == (False, UNAVAILABLE_ERROR)
assert send.call_count == 3
+def test_connection_failure_reports_unavailable() -> None:
+ with patch.object(Session, "send", side_effect=RequestException("boom")):
+ assert validate_credentials("fake-api-key", "123", 1) == (False, UNAVAILABLE_ERROR)
+
+
@pytest.mark.parametrize("project_id", ["", "../456", "123?key=other", "https://example.com", "123", "123/456"])
def test_invalid_project_id_never_makes_a_request(project_id: str) -> None:
with patch.object(Session, "send") as send: