: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(tiktok-ads): explain a missing token scope instead of showing tiktok's raw error (#111806)
Tom Owers8b79d8409331
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/source.pyindex ca055ec0f55c..82ca42bdf6cf 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/source.py@@ -42,6 +42,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_adsTIKTOK_AUTH_ERROR_CODES,TIKTOK_CREATIVE_PERMISSION_DENIED_FRAGMENTS,TIKTOK_CREATIVE_PERMISSION_DENIED_MESSAGE,+ TIKTOK_MISSING_SCOPE_FRAGMENT,+ TIKTOK_MISSING_SCOPE_MESSAGE,TIKTOK_NON_RETRYABLE_ERROR_PREFIX,TIKTOK_TRANSIENT_ERROR_CODES,TIKTOK_TRANSIENT_ERROR_MESSAGE,@@ -75,6 +77,7 @@ class TikTokAdsSource(ResumableSource[TikTokAdsSourceConfig, TikTokAdsResumeConf# Must precede TIKTOK_NON_RETRYABLE_ERROR_PREFIX: a denial matches both keys, and# `external_data_job` takes the first match in dict order, discarding it when None.**dict.fromkeys(TIKTOK_CREATIVE_PERMISSION_DENIED_FRAGMENTS, TIKTOK_CREATIVE_PERMISSION_DENIED_MESSAGE),+ TIKTOK_MISSING_SCOPE_FRAGMENT: TIKTOK_MISSING_SCOPE_MESSAGE,# Other TikTok client errors not in the retryable code set (e.g. 40001 — the advertiser# doesn't exist or has been deleted). The paginator raises these with this exact# prefix; retrying cannot recover, so fail the job fast. The raw message is kept asdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/test/test_tiktok_ads_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/test/test_tiktok_ads_source.pyindex aa56393df2e3..b97f94ceae43 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/test/test_tiktok_ads_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/test/test_tiktok_ads_source.py@@ -27,6 +27,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.generated_)from products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_ads.source import TikTokAdsSourcefrom products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_ads.utils import (+ TIKTOK_CREATIVE_PERMISSION_DENIED_MESSAGE,+ TIKTOK_MISSING_SCOPE_MESSAGE,TIKTOK_NON_RETRYABLE_ERROR_PREFIX,TIKTOK_TRANSIENT_ERROR_MESSAGE,TikTokAdsAPIError,@@ -97,11 +99,30 @@ class TestTikTokAdsSource:@parameterized.expand([- ("video", "advertiser does not grant you /file/video/ad/search/:GET permission"),- ("image", "advertiser does not grant you /file/image/ad/search/:GET permission"),+ (+ "video",+ "advertiser does not grant you /file/video/ad/search/:GET permission",+ TIKTOK_CREATIVE_PERMISSION_DENIED_MESSAGE,+ ),+ (+ "image",+ "advertiser does not grant you /file/image/ad/search/:GET permission",+ TIKTOK_CREATIVE_PERMISSION_DENIED_MESSAGE,+ ),+ (+ "report_scope",+ "Permission error: The access token lacks the required scope for endpoint "+ "'/report/integrated/get/(method=GET)'. Please first check if the request method is correct.",+ TIKTOK_MISSING_SCOPE_MESSAGE,+ ),+ (+ "campaign_scope",+ "Permission error: The access token lacks the required scope for endpoint '/campaign/get/(method=GET)'.",+ TIKTOK_MISSING_SCOPE_MESSAGE,+ ),])- def test_creative_permission_denied_surfaces_friendly_message(self, name, message):+ def test_permission_denied_surfaces_friendly_message(self, name, message, expected):"""Fails if the dict entries are reordered, which would shadow this message with None."""error_message = f"{TIKTOK_NON_RETRYABLE_ERROR_PREFIX} {message} (code: 40001)"@@ -112,9 +133,7 @@ class TestTikTokAdsSource:]assert friendly, "permission denial matched no non-retryable pattern"- assert friendly[0] is not None, "generic prefix shadowed the creative-permission message"- assert "creative_videos" in friendly[0]- assert "creative_images" in friendly[0]+ assert friendly[0] == expected@parameterized.expand([diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/utils.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/utils.pyindex 904562352ffc..33c0e21cfed7 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/utils.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/utils.py@@ -145,6 +145,17 @@ TIKTOK_CREATIVE_PERMISSION_DENIED_MESSAGE = ()+# TikTok's 40001 wording for a token issued without a scope one endpoint needs. Its own text tells an+# app developer to reauthorize their API app, which a customer can't act on and which names the+# endpoint and error code. Reconnecting issues a new token with the scopes PostHog's app holds.+TIKTOK_MISSING_SCOPE_FRAGMENT = "The access token lacks the required scope"++TIKTOK_MISSING_SCOPE_MESSAGE = (+ "TikTok denied access to this table because your connection is missing a permission. Reconnect "+ "your TikTok Ads integration and approve every permission TikTok asks for."+)++class TikTokAdsAPIError(Exception):"""A TikTok API failure, carrying the body `code` as `api_code` so callers can branch on it: thepipeline retries all of these, while the account-listing endpoint maps the auth and transient