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(ahrefs): tell users to re-enable the sync after api units run out (#123321)

Tom Owers7371ae22f1ef
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/ahrefs.py b/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/ahrefs.py
index b6a5c1f8b7c7..3520d0d60585 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/ahrefs.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/ahrefs.py
@@ -7,10 +7,10 @@ from requests import Request, Response
from products.warehouse_sources.backend.temporal.data_imports.sources.ahrefs.settings import (
API_BASE_URL,
+ CREDENTIAL_ERRORS,
ENDPOINTS,
HISTORY_DAYS,
MAX_COMPETITORS,
- NON_RETRYABLE_ERRORS,
TOP_DOMAINS_COUNT,
TOP_DOMAINS_PAGE_ROWS,
EndpointSettings,
@@ -73,7 +73,7 @@ def validate_credentials(api_key: str) -> tuple[bool, str | None]:
)
)
except ValueError as error:
- message = NON_RETRYABLE_ERRORS.get(str(error))
+ message = CREDENTIAL_ERRORS.get(str(error))
if message is None:
raise
return False, message
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/settings.py
index ca6ff1a20bb6..491545300b15 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/settings.py
@@ -299,6 +299,11 @@ INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
AUTH_ERROR = "Ahrefs authentication failed. Check your API key in Account settings > API keys."
ACCESS_ERROR = "Ahrefs denied access. Check your plan, project access, and verified ownership for page data."
QUOTA_ERROR = "Ahrefs API units are exhausted. Increase your API key or workspace limit, or wait for renewal."
+# A matched sync error turns the table's sync off, so renewed units alone don't start it again.
+QUOTA_SYNC_ERROR = (
+ "Your Ahrefs API units ran out, so this sync is off. Raise your API key or workspace unit limit, "
+ "or wait for the units to renew, then re-enable the sync."
+)
PROJECT_NOT_FOUND_ERROR = (
"Ahrefs returned no target for the Site Audit project. Check the project ID and that the project has a target URL."
)
@@ -306,9 +311,11 @@ PROJECT_NOT_FOUND_ERROR = (
NON_RETRYABLE_ERRORS: dict[str, str | None] = {
"ahrefs_invalid_api_key": AUTH_ERROR,
"ahrefs_access_denied": ACCESS_ERROR,
- "ahrefs_quota_exceeded": QUOTA_ERROR,
+ "ahrefs_quota_exceeded": QUOTA_SYNC_ERROR,
"ahrefs_project_not_found": PROJECT_NOT_FOUND_ERROR,
"401 Client Error": AUTH_ERROR,
"403 Client Error": ACCESS_ERROR,
- "402 Client Error": QUOTA_ERROR,
+ "402 Client Error": QUOTA_SYNC_ERROR,
}
+
+CREDENTIAL_ERRORS: dict[str, str | None] = {**NON_RETRYABLE_ERRORS, "ahrefs_quota_exceeded": QUOTA_ERROR}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/tests/test_ahrefs.py b/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/tests/test_ahrefs.py
index 520bef2437f2..8a54d5837bdd 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/tests/test_ahrefs.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/ahrefs/tests/test_ahrefs.py
@@ -298,18 +298,18 @@ def test_page_sample_cap(count: int) -> None:
@pytest.mark.parametrize(
- ("status", "body", "message"),
+ ("status", "body", "message", "sync_message"),
[
- (400, {"error": "API units limit exceeded"}, "API units are exhausted"),
- (402, {"error": "Payment required"}, "API units are exhausted"),
+ (400, {"error": "API units limit exceeded"}, "API units are exhausted", "then re-enable the sync"),
+ (402, {"error": "Payment required"}, "API units are exhausted", "then re-enable the sync"),
],
)
-def test_credential_and_sync_error_mapping(status: int, body: object, message: str) -> None:
+def test_credential_and_sync_error_mapping(status: int, body: object, message: str, sync_message: str) -> None:
answer = ScriptedResponse(status=status, json=body)
with scripted_network([answer]) as network:
valid, error = validate_credentials("test-key")
assert valid is False
- assert error is not None and message in error
+ assert error is not None and message in error and sync_message not in error
assert len(network.requests_log) == 1
source = AhrefsSource()
@@ -318,7 +318,7 @@ def test_credential_and_sync_error_mapping(status: int, body: object, message: s
)
assert isinstance(result.raised, ValueError)
mapped = source.get_non_retryable_errors()[str(result.raised)]
- assert mapped is not None and message in mapped
+ assert mapped is not None and sync_message in mapped
assert len(result.requests) == 1