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(apple_search_ads): recognize 401 with empty reason phrase as non-retryable (#127473)

Tom Owers58cfd10e8fe1
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/source.py
index 06ab2e0f2bf8..bad86a320e0b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/source.py
@@ -113,6 +113,8 @@ class AppleSearchAdsSource(
"400 Client Error: Bad Request for url: https://appleid.apple.com/auth/oauth2/token": "Apple rejected the signed client secret. Check your client ID, team ID, key ID and private key.",
"401 Client Error: Unauthorized for url: https://appleid.apple.com/auth/oauth2/token": "Apple rejected the signed client secret. Check your client ID, team ID, key ID and private key.",
"401 Client Error: Unauthorized for url: https://api.ads.apple.com": "Apple rejected the access token. Your API client may have been removed. Create a new one in Apple Ads and reconnect this source.",
+ # Apple occasionally returns an empty HTTP reason phrase for the same 401 condition.
+ "401 Client Error: for url: https://api.ads.apple.com": "Apple rejected the access token. Your API client may have been removed. Create a new one in Apple Ads and reconnect this source.",
"403 Client Error: Forbidden for url: https://api.ads.apple.com": "Apple denied access to this ad account. Check that the API user has the API Account Read Only role for the ad account ID you entered.",
"404 Client Error: Not Found for url: https://api.ads.apple.com": "Apple could not find this ad account. Check the ad account ID, which you can read from `adAccount.id` in Apple's Get User ACL endpoint.",
"400 Client Error: Bad Request for url: https://api.searchads.apple.com": "Apple rejected a reporting request. Some campaign types don't support keyword reporting. If the error persists, remove the keyword_report table or check your campaign types in Apple Ads.",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py
index 173876438670..f535a641bfa2 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py
@@ -45,6 +45,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.apple_sear
from products.warehouse_sources.backend.temporal.data_imports.sources.apple_search_ads.source import (
AppleSearchAdsSource,
)
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import error_message_matches
from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager
from products.warehouse_sources.backend.temporal.data_imports.sources.common.testing import (
DriveResult,
@@ -52,6 +53,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.tes
ScriptedResponse,
SourceDriver,
always,
+ http_response,
scripted_network,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.applesearchads import (
@@ -180,6 +182,13 @@ def _driver() -> SourceDriver:
return SourceDriver(AppleSearchAdsSource(), config)
+class _EmptyReasonResponse(ScriptedResponse):
+ """A response whose status line has no reason phrase, which Apple sometimes sends."""
+
+ def to_bytes(self) -> bytes:
+ return http_response(f"{self.status} ".encode(), b"", [])
+
+
def _api_requests(result: DriveResult) -> list[RecordedRequest]:
return [request for request in result.requests if request.url != APPLE_OAUTH_TOKEN_URL]
@@ -306,6 +315,16 @@ class TestAppleSearchAdsTransport:
assert isinstance(result.raised, HTTPError)
+ @parameterized.expand([("legacy_bad_request", V5, 400), ("platform_unauthorized", V1, 401)])
+ def test_an_error_without_a_reason_phrase_is_non_retryable(self, _name: str, api_version: str, status: int) -> None:
+ def answer(request: RecordedRequest) -> ScriptedResponse:
+ return _token_response() if request.url == APPLE_OAUTH_TOKEN_URL else _EmptyReasonResponse(status=status)
+
+ result = _driver().run("campaigns", answer, api_version=api_version)
+
+ assert isinstance(result.raised, HTTPError)
+ assert error_message_matches(str(result.raised), AppleSearchAdsSource().get_non_retryable_errors().keys())
+
@parameterized.expand([(V5,), (V1,)])
def test_entity_pagination_advances_the_offset_and_checkpoints_between_pages(self, api_version: str) -> None:
first_page = [{"id": index} for index in range(PAGE_SIZE)]