: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(zoho_crm): stop defaulting edition-gated modules to sync (#128063)
Tom Owers0308918c0f92
diff --git a/products/warehouse_sources/sources/zoho_crm/settings.py b/products/warehouse_sources/sources/zoho_crm/settings.pyindex 304e287d1e64..3b704478fe71 100644--- a/products/warehouse_sources/sources/zoho_crm/settings.py+++ b/products/warehouse_sources/sources/zoho_crm/settings.py@@ -53,8 +53,8 @@ ZOHO_CRM_ENDPOINTS: dict[str, ZohoCRMEndpointConfig] = {"Calls": _module("Calls"),"Notes": _module("Notes"),"Products": _module("Products"),- "Vendors": _module("Vendors"),# Inventory and support modules — edition/setup dependent.+ "Vendors": _module("Vendors", should_sync_default=False),"Price_Books": _module("Price_Books", should_sync_default=False),"Quotes": _module("Quotes", should_sync_default=False),"Sales_Orders": _module("Sales_Orders", should_sync_default=False),diff --git a/products/warehouse_sources/sources/zoho_crm/source.py b/products/warehouse_sources/sources/zoho_crm/source.pyindex 0a4f1967d1e3..e0e94b9f335c 100644--- a/products/warehouse_sources/sources/zoho_crm/source.py+++ b/products/warehouse_sources/sources/zoho_crm/source.py@@ -81,6 +81,7 @@ class ZohoCRMSource(ResumableSource[ZohoCRMSourceConfig, ZohoCRMResumeConfig]):"401 Client Error: for url": "Your Zoho CRM access token is invalid or expired. Reconnect the source to issue a new one.","403 Client Error: Forbidden for url": "Your Zoho CRM token is missing a scope for this module. Re-authorize with the ZohoCRM.modules.ALL, ZohoCRM.settings.fields.READ, and ZohoCRM.settings.modules.READ scopes.","403 Client Error: for url": "Your Zoho CRM token is missing a scope for this module. Re-authorize with the ZohoCRM.modules.ALL, ZohoCRM.settings.fields.READ, and ZohoCRM.settings.modules.READ scopes.",+ "INVALID_MODULE": "Zoho CRM doesn't have this module enabled for your account. Turn off this table in the source's schema settings, or enable the module in Zoho CRM.","Non-JSON response from": "Zoho CRM returned a non-JSON response, for example an HTML error or login page, instead of data. Check that the data center you picked matches your Zoho account, then try again.",}diff --git a/products/warehouse_sources/sources/zoho_crm/tests/test_zoho_crm.py b/products/warehouse_sources/sources/zoho_crm/tests/test_zoho_crm.pyindex 7c128a27a989..3a9845553c2e 100644--- a/products/warehouse_sources/sources/zoho_crm/tests/test_zoho_crm.py+++ b/products/warehouse_sources/sources/zoho_crm/tests/test_zoho_crm.py@@ -5,6 +5,8 @@ from typing import Any, Optionalimport pytestfrom unittest import mock+from requests.exceptions import HTTPError+from products.warehouse_sources.sources.sdk import RESTClientNonRetryableError, RESTClientRetryableErrorfrom products.warehouse_sources.sources.sdk.testing import (DriveResult,@@ -24,6 +26,7 @@ from products.warehouse_sources.sources.zoho_crm.zoho_crm import (MAX_PAGE,PAGE_SIZE,REFRESH_TOKEN_REJECTED_MESSAGE,+ ZohoCRMApiError,ZohoCRMAuthError,ZohoCRMClient,ZohoCRMResumeConfig,@@ -212,6 +215,41 @@ class TestReadableFieldNames:with scripted_network(_script([_undecodable_response(b"")])):assert readable_field_names(_client(), "v8", "Leads") == []+ @pytest.mark.parametrize(+ ("body", "expected_detail"),+ [+ (+ {"code": "INVALID_MODULE", "message": "the module name given seems to be invalid"},+ "INVALID_MODULE: the module name given seems to be invalid",+ ),+ ({"code": "INVALID_MODULE"}, "INVALID_MODULE"),+ ({"message": "no code here"}, "no code here"),+ ],+ )+ def test_structured_400_surfaces_zohos_code_and_message(self, body: dict[str, Any], expected_detail: str) -> None:+ # A bare raise_for_status() discards this body, so an org missing an edition-gated+ # module (e.g. Vendors, without Zoho's Inventory feature) is indistinguishable from any+ # other 400 — see the should_sync_default comment in settings.py.+ with scripted_network(_script([_response(400, body)])):+ with pytest.raises(ZohoCRMApiError, match=expected_detail):+ readable_field_names(_client(), "v8", "Vendors")++ def test_400_without_a_structured_body_stays_a_plain_http_error(self) -> None:+ with scripted_network(_script([ScriptedResponse(status=400, body=b"not json")])):+ with pytest.raises(HTTPError) as ctx:+ readable_field_names(_client(), "v8", "Leads")++ assert not isinstance(ctx.value, ZohoCRMApiError)++ def test_invalid_module_error_matches_get_non_retryable_errors(self) -> None:+ body = {"code": "INVALID_MODULE", "message": "the module name given seems to be invalid"}+ with scripted_network(_script([_response(400, body)])):+ with pytest.raises(ZohoCRMApiError) as ctx:+ readable_field_names(_client(), "v8", "Vendors")++ non_retryable = ZohoCRMSource().get_non_retryable_errors()+ assert any(pattern.lower() in str(ctx.value).lower() for pattern in non_retryable)+class TestGetRows:def test_incremental_sync_filters_and_sorts_on_the_cursor_field(self) -> None:@@ -304,6 +342,15 @@ class TestGetRows:assert isinstance(result.raised, RESTClientRetryableError)+ def test_structured_400_on_records_page_surfaces_zohos_detail(self) -> None:+ # A module can be disabled between the fields-metadata call and the first records page+ # of the same sync, so this call site needs the same detail as readable_field_names.+ body = {"code": "INVALID_MODULE", "message": "the module name given seems to be invalid"}+ result = _run("Vendors", [_fields_response(1), _response(400, body)])++ assert isinstance(result.raised, ZohoCRMApiError)+ assert "INVALID_MODULE: the module name given seems to be invalid" in str(result.raised)+class TestValidateCredentials:def test_valid_when_the_modules_probe_succeeds(self) -> None:diff --git a/products/warehouse_sources/sources/zoho_crm/tests/test_zoho_crm_source.py b/products/warehouse_sources/sources/zoho_crm/tests/test_zoho_crm_source.pyindex d38c319fb1aa..863d87d44020 100644--- a/products/warehouse_sources/sources/zoho_crm/tests/test_zoho_crm_source.py+++ b/products/warehouse_sources/sources/zoho_crm/tests/test_zoho_crm_source.py@@ -135,3 +135,20 @@ def test_sync_token_failure_shows_the_message_for_its_error_code(error_code: str]assert friendly[0] == expected+++def test_edition_dependent_modules_do_not_sync_by_default() -> None:+ # Each of these only exists on some Zoho CRM editions (Inventory), so Zoho answers+ # INVALID_MODULE for an org that never enabled it. A default-synced module fails every first+ # sync for such an org instead of just sitting unticked.+ for name in (+ "Vendors",+ "Price_Books",+ "Quotes",+ "Sales_Orders",+ "Purchase_Orders",+ "Invoices",+ "Cases",+ "Solutions",+ ):+ assert ZOHO_CRM_ENDPOINTS[name].should_sync_default is Falsediff --git a/products/warehouse_sources/sources/zoho_crm/zoho_crm.py b/products/warehouse_sources/sources/zoho_crm/zoho_crm.pyindex f1d8854fe271..c5238436aee2 100644--- a/products/warehouse_sources/sources/zoho_crm/zoho_crm.py+++ b/products/warehouse_sources/sources/zoho_crm/zoho_crm.py@@ -80,6 +80,34 @@ TOKEN_ERROR_MESSAGES = {TOKEN_REFRESH_FAILED_PREFIX = "Zoho CRM token refresh failed"+class ZohoCRMApiError(requests.HTTPError):+ """A structured 4xx Zoho CRM answers with its own `code`/`message` envelope.++ `raise_for_status()` alone discards the body, producing a body-less "400 Client Error:+ for url: ..." that can't be told apart from any other 400 — for example INVALID_MODULE,+ which Zoho returns for a module the org never enabled, from a scope or parameter problem.+ """+++def _with_zoho_error_detail(error: requests.HTTPError) -> requests.HTTPError:+ """Fold Zoho's own `code`/`message` into `error`, when the response body carries one."""+ response = error.response+ try:+ body = response.json() if response is not None else None+ except ValueError:+ body = None+ if not isinstance(body, dict):+ return error++ code = body.get("code")+ message = body.get("message") or ""+ if not code and not message:+ return error++ detail = f"{code}: {message}".rstrip(": ") if code else message+ return ZohoCRMApiError(f"{error} ({detail})", response=response)++class ZohoCRMAuthError(Exception):def __init__(self, error_code: Optional[str]) -> None:super().__init__(f"{TOKEN_REFRESH_FAILED_PREFIX}: {error_code or 'no access token returned'}")@@ -231,7 +259,10 @@ class ZohoCRMClient:def readable_field_names(client: ZohoCRMClient, api_version: str, module: str) -> list[str]:"""Field API names Get Records can project for `module`, from the fields metadata API."""- response = client.get(f"/crm/{api_version}/settings/fields", params={"module": module})+ try:+ response = client.get(f"/crm/{api_version}/settings/fields", params={"module": module})+ except requests.HTTPError as e:+ raise _with_zoho_error_detail(e) from eif response.status_code in NO_CONTENT_STATUSES:return []@@ -259,7 +290,10 @@ def _fetch_page(params: dict[str, str],headers: dict[str, str],) -> tuple[list[dict[str, Any]], dict[str, Any]]:- response = client.get(f"/crm/{api_version}/{config.path}", params=params, headers=headers)+ try:+ response = client.get(f"/crm/{api_version}/{config.path}", params=params, headers=headers)+ except requests.HTTPError as e:+ raise _with_zoho_error_detail(e) from eif response.status_code in NO_CONTENT_STATUSES:return [], {}