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(warehouse-sources): classify exhausted HubSpot 401 as reconnect (#91331)

posthog[bot]fef0e0a7e338
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/source.py
index 9b1ad7cf49b9..71f7ee1f5eb4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/source.py
@@ -132,11 +132,14 @@ class HubspotSource(ResumableSource[HubspotSourceConfig | HubspotSourceOldConfig
**missing_scope_errors,
"missing or invalid refresh token": "Your HubSpot connection is invalid or expired. Please reconnect it.",
"missing or unknown hub id": None,
- # Raised by helpers._get after tenacity exhausts all 5 retry attempts where every attempt
- # got a 401: the code refreshes the access token each time but HubSpot keeps rejecting it.
- # A persistent 401 after token refresh means the OAuth grant is fundamentally broken
- # (revoked, app deleted, permissions withdrawn) — Temporal retrying the activity can't help.
- "Hubspot API 401 - refreshed token, retrying:": "Your HubSpot credentials are no longer authorized. Please reconnect your HubSpot account and ensure it has the required permissions, then try again.",
+ # A 401 means the OAuth grant can't read the requested object (token revoked, or the
+ # connected app lost a scope like `crm.objects.companies.read`). Every fetch loop
+ # (fetch_data._get, fetch_page, v4 associations, search) refreshes the access token on a
+ # 401 and re-raises HubspotRetryableError, so tenacity retries with a fresh token. Five
+ # straight 401s after a good refresh means the grant is dead, not a transient blip, so
+ # retrying can't recover. Match the shared message fragment all four loops emit, not the
+ # per-loop prefix or the volatile URL.
+ "401 - refreshed token, retrying": "Your HubSpot credentials are no longer authorized. Please reconnect your HubSpot account and ensure it has the required permissions, then try again.",
# HubSpot's CRM API may also surface 401 through raise_for_status() in other fetch paths.
# Match the stable host prefix, not the per-object URL path, which varies by endpoint.
"401 Client Error: Unauthorized for url: https://api.hubapi.com": "Your HubSpot credentials are no longer authorized. Please reconnect your HubSpot account and ensure it has the required permissions, then try again.",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_source_routing.py b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_source_routing.py
index 84a1d5dba3fc..4e879d38eb17 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_source_routing.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/hubspot/test/test_source_routing.py
@@ -396,13 +396,15 @@ class TestApiVersion:
@pytest.mark.parametrize(
"error_msg",
[
- # helpers._get exhausted all 5 tenacity retries on a 401 — token refresh succeeded each
- # time but HubSpot kept rejecting the request, indicating broken OAuth credentials.
- "Hubspot API 401 - refreshed token, retrying: url=https://api.hubapi.com/crm/v3/properties/tickets",
- "Hubspot API 401 - refreshed token, retrying: url=https://api.hubapi.com/crm/v3/properties/contacts",
- # raise_for_status() 401/403 from other fetch paths
+ # Each fetch loop refreshes the token on a 401 and re-raises this after tenacity's 5 attempts
+ "Hubspot API 401 - refreshed token, retrying: url=https://api.hubapi.com/crm/v3/properties/companies",
+ "Hubspot API 401 - refreshed token, retrying: url=https://api.hubapi.com/crm/v3/objects/deals",
+ "Hubspot v4 associations 401 - refreshed token, retrying: "
+ "url=https://api.hubapi.com/crm/v4/associations/contacts/deals/batch/read",
+ "Hubspot search 401 - refreshed token, retrying: url=https://api.hubapi.com/crm/v3/objects/contacts/search",
+ # raise_for_status() 401 from other fetch paths
"401 Client Error: Unauthorized for url: https://api.hubapi.com/crm/v3/properties/companies",
- "401 Client Error: Unauthorized for url: https://api.hubapi.com/crm/v3/properties/deals",
+ # raise_for_hubspot_status maps a 403 to this verbatim
"403 Client Error: Forbidden for url: https://api.hubapi.com/crm/v3/objects/contacts",
],
)