: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): mark Shopify access token 4xx as non-retryable (#61605)
Tom Owersba35b0c31202
diff --git a/posthog/temporal/data_imports/sources/shopify/shopify.py b/posthog/temporal/data_imports/sources/shopify/shopify.pyindex 2a48d365eeac..13bd9ca558f0 100644--- a/posthog/temporal/data_imports/sources/shopify/shopify.py+++ b/posthog/temporal/data_imports/sources/shopify/shopify.py@@ -32,6 +32,14 @@ PHASE_ALL = "all"PHASE_EARLIEST = "earliest"PHASE_LATEST = "latest"+# Raised when Shopify's OAuth token endpoint returns a 4xx — the app credentials are+# invalid or the app was uninstalled, so re-auth is the only fix. `ShopifySource.+# get_non_retryable_errors` matches on this exact text to fail the job fast.+SHOPIFY_ACCESS_TOKEN_AUTH_ERROR = (+ "Failed to retrieve Shopify access token: the app credentials are invalid or the "+ "app was uninstalled. Please reconnect your Shopify integration."+)+@dataclasses.dataclassclass ShopifyResumeConfig:@@ -156,6 +164,11 @@ def _get_shopify_access_token(shopify_store_id: str, shopify_client_id: str, sho}access_res = make_tracked_session().post(access_token_url, data=access_data)if not access_res.ok:+ # A 4xx means the app credentials are invalid/revoked (e.g. the app was+ # uninstalled) — re-auth is the only fix, so surface a non-retryable message.+ # 429 (rate limit) and 5xx are transient and stay retryable via the generic message.+ if 400 <= access_res.status_code < 500 and access_res.status_code != 429:+ raise Exception(f"{SHOPIFY_ACCESS_TOKEN_AUTH_ERROR} (HTTP {access_res.status_code})")raise Exception(f"Failed to retrieve Shopify access token: {access_res}")return access_res.json()["access_token"]diff --git a/posthog/temporal/data_imports/sources/shopify/source.py b/posthog/temporal/data_imports/sources/shopify/source.pyindex 1e7818145be5..167d73dfed5e 100644--- a/posthog/temporal/data_imports/sources/shopify/source.py+++ b/posthog/temporal/data_imports/sources/shopify/source.py@@ -16,6 +16,7 @@ from posthog.temporal.data_imports.sources.generated_configs import ShopifySourcfrom posthog.temporal.data_imports.sources.shopify.constants import SHOPIFY_GRAPHQL_OBJECTSfrom posthog.temporal.data_imports.sources.shopify.settings import ENDPOINT_CONFIGSfrom posthog.temporal.data_imports.sources.shopify.shopify import (+ SHOPIFY_ACCESS_TOKEN_AUTH_ERROR,ShopifyPermissionError,ShopifyResumeConfig,shopify_source,@@ -31,6 +32,13 @@ class ShopifySource(ResumableSource[ShopifySourceConfig, ShopifyResumeConfig]):def source_type(self) -> ExternalDataSourceType:return ExternalDataSourceType.SHOPIFY+ def get_non_retryable_errors(self) -> dict[str, str | None]:+ return {+ # 4xx from Shopify's OAuth token endpoint — invalid/revoked app credentials.+ # Retrying cannot recover; the user must reconnect the integration.+ SHOPIFY_ACCESS_TOKEN_AUTH_ERROR: SHOPIFY_ACCESS_TOKEN_AUTH_ERROR,+ }+@propertydef get_source_config(self) -> SourceConfig:return SourceConfig(diff --git a/posthog/temporal/data_imports/sources/shopify/tests/test_access_token.py b/posthog/temporal/data_imports/sources/shopify/tests/test_access_token.pynew file mode 100644index 000000000000..35a4b3e6bb54--- /dev/null+++ b/posthog/temporal/data_imports/sources/shopify/tests/test_access_token.py@@ -0,0 +1,55 @@+import pytest+from unittest import mock++from posthog.temporal.data_imports.sources.shopify.shopify import (+ SHOPIFY_ACCESS_TOKEN_AUTH_ERROR,+ _get_shopify_access_token,+)+from posthog.temporal.data_imports.sources.shopify.source import ShopifySource+++def _mock_response(status_code: int, ok: bool, json_data: dict | None = None) -> mock.MagicMock:+ response = mock.MagicMock()+ response.status_code = status_code+ response.ok = ok+ response.json.return_value = json_data or {}+ return response+++def _patched_token_call(response: mock.MagicMock):+ return mock.patch(+ "posthog.temporal.data_imports.sources.shopify.shopify.make_tracked_session",+ return_value=mock.MagicMock(post=mock.MagicMock(return_value=response)),+ )++[email protected]("status_code", [400, 401, 403, 404])+def test_get_access_token_4xx_is_non_retryable(status_code):+ with _patched_token_call(_mock_response(status_code, ok=False)):+ with pytest.raises(Exception) as exc_info:+ _get_shopify_access_token("store", "client-id", "client-secret")++ error_message = str(exc_info.value)+ assert SHOPIFY_ACCESS_TOKEN_AUTH_ERROR in error_message+ patterns = ShopifySource().get_non_retryable_errors()+ assert any(pattern in error_message for pattern in patterns), (+ f"4xx token error '{error_message}' should match a non-retryable pattern"+ )++[email protected]("status_code", [429, 500, 502, 503])+def test_get_access_token_transient_stays_retryable(status_code):+ with _patched_token_call(_mock_response(status_code, ok=False)):+ with pytest.raises(Exception) as exc_info:+ _get_shopify_access_token("store", "client-id", "client-secret")++ error_message = str(exc_info.value)+ patterns = ShopifySource().get_non_retryable_errors()+ assert not any(pattern in error_message for pattern in patterns), (+ f"transient token error '{error_message}' should remain retryable"+ )+++def test_get_access_token_success_returns_token():+ with _patched_token_call(_mock_response(200, ok=True, json_data={"access_token": "tok"})):+ assert _get_shopify_access_token("store", "client-id", "client-secret") == "tok"