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-warehouse): tolerate Apple's misleading 400 on sales reports too (#111980)

Tom Owers4bf788cba650
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py
index 7d066a4c316a..c8f339e57272 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/app_store_connect.py
@@ -848,16 +848,13 @@ def _parse_report(payload: bytes, report_date: date, failures: _ParseFailureCoun
return rows
-# A subscription-family report day with no data comes back as a 400 whose body carries one of these
-# markers. A 400 matching none of them, and not the vendor-number marker below, is a genuinely
-# malformed request rather than a quiet day.
+# Apple can return 400 instead of 404 when a report day has no data.
_EMPTY_REPORT_400_MARKERS = (
"there were no results",
"no results for the request",
)
-# Apple's long-standing misleading wording for an empty subscription day, which it also returns for a
-# vendor number it doesn't know. Ambiguous on its own, so `_VendorNumberCheck` settles it.
+# This wording is ambiguous: Apple also uses it for an unknown vendor number.
_VENDOR_NUMBER_400_MARKER = "invalid vendor number"
@@ -869,11 +866,8 @@ def _apple_error_text(error: _AppleApiError) -> str:
class _ToleratedReportDays:
"""Counts report days Apple answered with a tolerated "no data" status, keeping one sample error.
- A 404 is Apple's "no activity for this date" for the SALES report; the subscription report
- families return a 400 for the same condition instead, and Apple words that 400 the same way it
- words a genuinely bad request. A whole run of tolerated days that never produced a row is logged
- when the walk ends, so an operator can tell an account with no data for this report from a
- request Apple keeps rejecting, rather than reading both as a quiet account.
+ A whole run of tolerated days that never produced a row is logged when the walk ends, so an
+ operator can distinguish an account with no data from a request Apple keeps rejecting.
"""
def __init__(self, logger: FilteringBoundLogger, config: AppStoreConnectEndpointConfig) -> None:
@@ -906,12 +900,9 @@ class _ToleratedReportDays:
class _VendorNumberCheck:
"""Resolves once per run whether Apple knows the configured vendor number.
- Apple answers a subscription-family day with no data and a vendor number it doesn't know with the
- same "Invalid vendor number specified" 400, so that response alone cannot tell a quiet account
- from a typo. The SALES report separates them: it answers 404 for a known vendor with no data on
- the date, and keeps the vendor-number 400 for one it doesn't know. An inconclusive check — a key
- without a Sales role, a network failure — reads as known, so a sync never fails on the check
- itself and the all-empty run warning stays the backstop.
+ Apple uses the same 400 for an empty day and an unknown vendor number. The SALES report usually
+ distinguishes them with a 404, but cannot independently verify a SALES request. An inconclusive
+ probe is treated as known so the check itself never fails a sync.
"""
def __init__(
@@ -996,11 +987,7 @@ def _fetch_report(
tolerate=config.missing_report_status_codes,
)
if response.status_code in config.missing_report_status_codes:
- # A 404 is always Apple's "no activity for this date" — normal for quiet days and for dates
- # before the app shipped. A tolerated 400 is the subscription-family equivalent, but Apple
- # reuses 400 for a genuinely malformed request and for a vendor number it doesn't know too,
- # so read the body: only a day Apple really has no data for is tolerated and counted, and
- # anything else fails loudly instead of masquerading as a quiet account across the lookback.
+ # Unlike 404, a tolerated 400 must be classified from its body.
if response.status_code == 400:
apple_error = _parse_apple_error(response)
text = _apple_error_text(apple_error)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/settings.py
index afaf7a2be8ec..e18a77b600a4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/settings.py
@@ -91,14 +91,8 @@ class AppStoreConnectEndpointConfig:
# the DAILY/SUMMARY combination of each report type.
report_version: str = ""
report_frequency: str = "DAILY"
- # Apple 404s a SALES report request for a date with no data. Subscription-family report types
- # (SUBSCRIPTION, SUBSCRIPTION_EVENT) instead 400 with a misleading "Invalid vendor number
- # specified" error for that same condition — a longstanding, publicly reported Apple API quirk,
- # usually not an actual credentials problem. A tolerated 400 is not swallowed blindly:
- # `_fetch_report` reads the body, so a genuinely malformed request (wrong version or sub type)
- # still fails loudly instead of reading as a quiet account. Apple words that same 400 for a
- # vendor number it doesn't know, so a sales-report check separates the two before the misleading
- # wording is tolerated across the whole lookback.
+ # Apple sometimes returns a misleading 400 instead of 404 for an empty report day. Endpoints that
+ # exhibit this quirk tolerate 400; `_fetch_report` classifies the response body before swallowing it.
missing_report_status_codes: tuple[int, ...] = (404,)
@@ -214,6 +208,7 @@ APP_STORE_CONNECT_ENDPOINTS: dict[str, AppStoreConnectEndpointConfig] = {
incremental_fields=[_REPORT_DATE_FIELD],
partition_key="report_date",
should_sync_default=False,
+ missing_report_status_codes=(404, 400),
),
# Daily active subscription counts by state and territory.
"subscription_reports": AppStoreConnectEndpointConfig(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py
index 8670fd217677..30c10171b9db 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/app_store_connect/tests/test_app_store_connect.py
@@ -1817,27 +1817,18 @@ class TestSalesReports:
assert api.report_dates("SUBSCRIPTION") == ["2026-03-02", "2026-03-03", "2026-03-04"]
@time_machine.travel("2026-03-05 09:00:00", tick=False)
- def test_sales_report_400_is_not_tolerated(self) -> None:
- # SALES reports don't carry the subscription-family quirk, so a 400 there is a real error and
- # must still surface rather than being silently treated as an empty day.
- session = MagicMock()
- bad_request = _report_response(None, missing_status_code=400)
- bad_request.raise_for_status.side_effect = Exception("400 Client Error: Bad Request")
- session.get.return_value = bad_request
+ def test_sales_report_vendor_number_400_fails_fast(self) -> None:
+ api = _FakeReportApi({}, sales_status_code=400)
- with patch(f"{MODULE}._make_session", return_value=session):
- with pytest.raises(Exception, match="400"):
- list(
- get_rows(
- issuer_id="issuer",
- key_id="KEY123",
- private_key=PRIVATE_KEY_PEM,
- vendor_number="85234567",
- endpoint="sales_reports",
- logger=MagicMock(),
- resumable_source_manager=_FakeManager(),
- )
- )
+ with pytest.raises(AppStoreConnectReportError, match="does not recognize the vendor number"):
+ _collect(
+ "sales_reports",
+ api,
+ _FakeManager(),
+ vendor_number="85234567",
+ should_use_incremental_field=True,
+ db_incremental_field_last_value=date(2026, 3, 2),
+ )
@time_machine.travel("2026-03-05 09:00:00", tick=False)
def test_subscription_report_unrecognized_400_fails_loudly(self) -> None: