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(hubspot): treat 401/403 from HubSpot API as non-retryable (#63346)

Tom Owers9ae9275b17bf
diff --git a/posthog/temporal/data_imports/sources/hubspot/source.py b/posthog/temporal/data_imports/sources/hubspot/source.py
index 04d45cd8569d..464951bc83a3 100644
--- a/posthog/temporal/data_imports/sources/hubspot/source.py
+++ b/posthog/temporal/data_imports/sources/hubspot/source.py
@@ -87,6 +87,13 @@ class HubspotSource(ResumableSource[HubspotSourceConfig | HubspotSourceOldConfig
return {
"missing or invalid refresh token": "Your HubSpot connection is invalid or expired. Please reconnect it.",
"missing or unknown hub id": None,
+ # HubSpot's CRM API returns 401/403 when the OAuth grant can't read the requested object
+ # (token revoked, or the connected app is missing a scope like `crm.objects.companies.read`).
+ # `fetch_data` already refreshes the access token once on a 401; if the retried request is
+ # still rejected, the credentials genuinely lack access and retrying can't recover. Match the
+ # stable host, not the per-object URL path (companies/deals/contacts/...), which varies.
+ "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.",
+ "403 Client Error: Forbidden for url: https://api.hubapi.com": "Your HubSpot credentials do not have permission to access this data. Please reconnect your HubSpot account and ensure it has the required permissions, then try again.",
# Raised by source_for_pipeline when the source config carries no refresh token at all
# (integration never connected or lost its token). Retrying cannot recover.
"Hubspot refresh token not found": "Your HubSpot connection is missing its refresh token. Please reconnect it.",
diff --git a/posthog/temporal/data_imports/sources/hubspot/test/test_source_routing.py b/posthog/temporal/data_imports/sources/hubspot/test/test_source_routing.py
index dc8a734bd647..278cecbecfea 100644
--- a/posthog/temporal/data_imports/sources/hubspot/test/test_source_routing.py
+++ b/posthog/temporal/data_imports/sources/hubspot/test/test_source_routing.py
@@ -235,3 +235,21 @@ def test_missing_token_error_is_non_retryable(error_msg: str) -> None:
assert any(pattern in error_msg for pattern in patterns), (
f"HubSpot error {error_msg!r} did not match any non-retryable pattern"
)
+
+
+ "error_msg",
+ [
+ # Raised by fetch_data when a token refresh succeeds but the retried request is still rejected
+ "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",
+ "403 Client Error: Forbidden for url: https://api.hubapi.com/crm/v3/objects/contacts",
+ ],
+)
+def test_unauthorized_error_is_non_retryable(error_msg: str) -> None:
+ """A 401/403 from the HubSpot API means the credentials/OAuth grant can't access the data —
+ retrying can't recover, so it must match a non-retryable pattern."""
+ patterns = HubspotSource().get_non_retryable_errors()
+ assert any(pattern in error_msg for pattern in patterns), (
+ f"HubSpot error {error_msg!r} did not match any non-retryable pattern"
+ )