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(shopify): show guidance when the setup token check fails (#106582)

Tom Owerse58a08af178f
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py
index b51cd8ecad64..60441039c578 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py
@@ -11,6 +11,7 @@ from structlog.types import FilteringBoundLogger
from tenacity import RetryCallState, retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
from posthog.dataclasses import frozen
+from posthog.exceptions_capture import capture_exception
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http import make_tracked_session
from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager
@@ -164,6 +165,20 @@ SHOPIFY_GRAPHQL_UNAUTHORIZED_ERROR_MESSAGE = (
"reconnect your Shopify integration."
)
+# The wizard shows these when the token check in `validate_credentials` fails. A supplied Admin API
+# access token skips the token endpoint, so this check is the first place a bad token or store id
+# surfaces, and the raw `requests` error names the store URL and the HTTP status.
+SHOPIFY_ACCESS_TOKEN_REJECTED_ERROR = (
+ "Shopify rejected your access token for this store. Copy the Admin API access token again "
+ "from the app installed on your store, then reconnect."
+)
+SHOPIFY_STORE_FROZEN_ERROR = (
+ "Your Shopify store is frozen because of an unpaid bill. Settle your balance in Shopify, then reconnect."
+)
+SHOPIFY_CREDENTIALS_CHECK_ERROR = (
+ "PostHog couldn't verify your Shopify credentials. Check your store id and credentials, then try again."
+)
+
@dataclasses.dataclass
class ShopifyResumeConfig:
@@ -738,12 +753,25 @@ def validate_credentials(
# A valid token can always read the shop resource.
try:
res = sess.post(api_url, json={"query": SHOPIFY_ACCESS_TOKEN_CHECK})
+ except requests.RequestException as e:
+ capture_exception(e)
+ raise Exception(SHOPIFY_CREDENTIALS_CHECK_ERROR) from e
+ if res.status_code == 401:
+ raise Exception(
+ SHOPIFY_ACCESS_TOKEN_REJECTED_ERROR if shopify_access_token else SHOPIFY_CREDENTIALS_CHECK_ERROR
+ )
+ if res.status_code == 402:
+ raise Exception(SHOPIFY_STORE_FROZEN_ERROR)
+ if res.status_code == 404:
+ raise Exception(SHOPIFY_STORE_NOT_FOUND_ERROR)
+ try:
res.raise_for_status()
data = res.json()
if "errors" in data:
- raise Exception(f"Failed to verify your Shopify credentials: {data['errors']}")
+ raise Exception(f"Shopify credential check returned errors: {_format_graphql_errors(data['errors'])}")
except Exception as e:
- raise Exception(f"Failed to verify your Shopify credentials: {e}")
+ capture_exception(e)
+ raise Exception(SHOPIFY_CREDENTIALS_CHECK_ERROR) from e
if resources is None:
return True
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_validate_credentials.py b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_validate_credentials.py
index 6f84398c57ce..5eff146641f8 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_validate_credentials.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_validate_credentials.py
@@ -1,17 +1,27 @@
from typing import Any
+import pytest
from unittest import mock
+import requests
+
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.shopify import (
ShopifyAuthMethodConfig,
ShopifySourceConfig,
)
+from products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify import (
+ SHOPIFY_ACCESS_TOKEN_REJECTED_ERROR,
+ SHOPIFY_CREDENTIALS_CHECK_ERROR,
+ SHOPIFY_STORE_FROZEN_ERROR,
+ SHOPIFY_STORE_NOT_FOUND_ERROR,
+)
from products.warehouse_sources.backend.temporal.data_imports.sources.shopify.source import ShopifySource
_TOKEN_PATH = (
"products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify._get_shopify_access_token"
)
-_SESSION_PATH = "products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify.make_tracked_session"
+_SHOPIFY_MODULE = "products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify"
+_SESSION_PATH = f"{_SHOPIFY_MODULE}.make_tracked_session"
def _access_denied(message: str) -> dict[str, Any]:
@@ -153,3 +163,48 @@ def test_a_configured_access_token_authenticates_without_minting_one():
assert (valid, error) == (True, None)
mint.assert_not_called()
assert session_factory.call_args.kwargs["headers"]["X-Shopify-Access-Token"] == "shpat_supplied"
+
+
+def _token_config() -> ShopifySourceConfig:
+ return ShopifySourceConfig(
+ shopify_store_id="my-store",
+ auth_method=ShopifyAuthMethodConfig(selection="access_token", shopify_access_token="shpat_supplied"),
+ )
+
+
+def _http_error_response(status_code: int, reason: str) -> requests.Response:
+ response = requests.Response()
+ response.status_code = status_code
+ response.reason = reason
+ response.url = "https://my-store.myshopify.com/admin/api/2026-07/graphql.json"
+ return response
+
+
+ "config,response,expected_error",
+ [
+ (_token_config(), _http_error_response(401, "Unauthorized"), SHOPIFY_ACCESS_TOKEN_REJECTED_ERROR),
+ (_config(), _http_error_response(401, "Unauthorized"), SHOPIFY_CREDENTIALS_CHECK_ERROR),
+ (_token_config(), _http_error_response(402, "Payment Required"), SHOPIFY_STORE_FROZEN_ERROR),
+ (_token_config(), _http_error_response(404, "Not Found"), SHOPIFY_STORE_NOT_FOUND_ERROR),
+ (_token_config(), _http_error_response(500, "Internal Server Error"), SHOPIFY_CREDENTIALS_CHECK_ERROR),
+ (
+ _token_config(),
+ mock.MagicMock(status_code=200, json=mock.MagicMock(return_value={"errors": "boom"})),
+ SHOPIFY_CREDENTIALS_CHECK_ERROR,
+ ),
+ ],
+)
+def test_a_failed_token_check_shows_guidance_instead_of_the_raw_error(
+ config: ShopifySourceConfig, response: Any, expected_error: str
+):
+ session = mock.MagicMock(post=mock.MagicMock(return_value=response))
+
+ with (
+ mock.patch(_TOKEN_PATH, return_value="tok"),
+ mock.patch(_SESSION_PATH, return_value=session),
+ mock.patch(f"{_SHOPIFY_MODULE}.capture_exception"),
+ ):
+ valid, error = ShopifySource().validate_credentials(config, team_id=1)
+
+ assert (valid, error) == (False, expected_error)