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(integrations): classify hubspot BAD_HUB and 429 oauth refresh failures (#72609)

Harley Alexanderafae06983f40
diff --git a/posthog/models/integration.py b/posthog/models/integration.py
index 5fc0ec795ad5..406e33c72f6f 100644
--- a/posthog/models/integration.py
+++ b/posthog/models/integration.py
@@ -112,6 +112,7 @@ REFRESH_FAILURE_REASON_INVALID_GRANT = "invalid_grant"
REFRESH_FAILURE_REASON_INVALID_CLIENT = "invalid_client"
REFRESH_FAILURE_REASON_HTTP_5XX = "http_5xx"
REFRESH_FAILURE_REASON_NETWORK = "network"
+REFRESH_FAILURE_REASON_RATE_LIMITED = "rate_limited"
REFRESH_FAILURE_REASON_OTHER = "other"
@@ -126,6 +127,17 @@ def oauth_refresh_failure_reason(status_code: int, body: dict, kind: str | None
# endpoint means the grant, not the request - match that exact shape for reddit only.
if kind == "reddit-ads" and status_code == 400 and error == 400:
return REFRESH_FAILURE_REASON_INVALID_GRANT
+ # HubSpot reports a grant whose portal (hub) was deleted or disconnected as
+ # `{"status": "BAD_HUB", "error": "access_denied", ...}` - no `invalid_grant` code, so
+ # without this mapping the row is retried forever instead of going terminal. Its
+ # `BAD_REFRESH_TOKEN` responses do carry `"error": "invalid_grant"` and need no special case.
+ if kind == "hubspot" and status_code < 500 and body.get("status") == "BAD_HUB":
+ return REFRESH_FAILURE_REASON_INVALID_GRANT
+ # Transient throttling, not a credential problem: the backoff cap synchronises failed
+ # integrations into retry herds that can trip a provider's per-second limit and take
+ # healthy refreshes in the same second down with them.
+ if status_code == 429:
+ return REFRESH_FAILURE_REASON_RATE_LIMITED
if status_code >= 500:
return REFRESH_FAILURE_REASON_HTTP_5XX
return REFRESH_FAILURE_REASON_OTHER
@@ -139,8 +151,8 @@ def record_refresh_failure(integration: "Integration", *, reason: str = REFRESH_
integration goes terminal and the sweep stops retrying entirely. The streak is tracked
separately from the total failure count and resets on any other reason, so one transient
invalid_grant amid e.g. a 5xx outage can't brick the integration. Other reasons
- (invalid_client, 5xx, network) never go terminal - a platform-side credential fix must let
- the fleet self-recover.
+ (invalid_client, 5xx, network, rate_limited) never go terminal - a platform-side credential
+ fix must let the fleet self-recover.
Returns "first"/"retry" for the metric's `attempt` label - a spike in first failures means
connections are newly breaking, regardless of retry noise.
diff --git a/posthog/models/test/test_integration_model.py b/posthog/models/test/test_integration_model.py
index ce7ab48eebe8..980a53197ce8 100644
--- a/posthog/models/test/test_integration_model.py
+++ b/posthog/models/test/test_integration_model.py
@@ -861,6 +861,30 @@ class TestOauthIntegrationModel(BaseTest):
("reddit_shape_on_other_kind", 400, {"message": "Bad Request", "error": 400}, "hubspot", "other"),
("reddit_5xx", 502, {"message": "Bad Gateway", "error": 502}, "reddit-ads", "http_5xx"),
("reddit_oauth_error_code", 400, {"error": "invalid_grant"}, "reddit-ads", "invalid_grant"),
+ (
+ "hubspot_dead_hub_shape",
+ 400,
+ {"status": "BAD_HUB", "message": "missing or unknown hub id", "error": "access_denied"},
+ "hubspot",
+ "invalid_grant",
+ ),
+ (
+ "hubspot_shape_on_other_kind",
+ 400,
+ {"status": "BAD_HUB", "error": "access_denied"},
+ "salesforce",
+ "other",
+ ),
+ ("hubspot_bad_hub_5xx_is_outage", 502, {"status": "BAD_HUB"}, "hubspot", "http_5xx"),
+ (
+ "hubspot_bad_refresh_token_still_oauth_code",
+ 400,
+ {"status": "BAD_REFRESH_TOKEN", "error": "invalid_grant"},
+ "hubspot",
+ "invalid_grant",
+ ),
+ ("rate_limited", 429, {"status": "error", "errorType": "RATE_LIMIT"}, "hubspot", "rate_limited"),
+ ("rate_limited_any_kind", 429, {}, None, "rate_limited"),
]
)
def test_oauth_refresh_failure_reason(self, _name, status_code, body, kind, expected):