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(data-warehouse): mark Salesforce expired refresh token as non-retryable (#61615)

Tom Owerse419e40b47a5
diff --git a/posthog/temporal/data_imports/sources/salesforce/source.py b/posthog/temporal/data_imports/sources/salesforce/source.py
index e688c1493167..f08761ec01f1 100644
--- a/posthog/temporal/data_imports/sources/salesforce/source.py
+++ b/posthog/temporal/data_imports/sources/salesforce/source.py
@@ -32,6 +32,11 @@ class SalesforceSource(ResumableSource[SalesforceSourceConfig, SalesforceResumeC
"400 Client Error: Bad Request for url": None,
"403 Client Error: Forbidden for url": None,
"inactive organization": None,
+ # SalesforceAuthRequestError.raise_from_response formats token-refresh failures as
+ # "<code> Client Error: <reason>: <error_description>", so the "... for url" patterns
+ # above never match it. Key off the stable error_description returned by Salesforce
+ # when the refresh token is expired/revoked — reconnecting is the only fix.
+ "expired access/refresh token": "Your Salesforce connection has expired or been revoked. Please reconnect the source.",
}
def get_schemas(
diff --git a/posthog/temporal/data_imports/sources/salesforce/test/test_auth.py b/posthog/temporal/data_imports/sources/salesforce/test/test_auth.py
index 17daced089d0..ccdfaeebcc51 100644
--- a/posthog/temporal/data_imports/sources/salesforce/test/test_auth.py
+++ b/posthog/temporal/data_imports/sources/salesforce/test/test_auth.py
@@ -98,3 +98,28 @@ def test_get_salesforce_access_token_from_code_raises_on_server_failure():
assert exc.value.response == response
assert "Server Error" in str(exc.value)
assert response_body in str(exc.value)
+
+
+def test_expired_refresh_token_error_is_non_retryable():
+ """The SalesforceAuthRequestError raised for an expired refresh token must match a
+ non-retryable pattern, otherwise the job retries a permanent auth failure forever."""
+ from posthog.temporal.data_imports.sources.salesforce.source import SalesforceSource
+
+ response = requests.Response()
+ response.status_code = 400
+ response._content = json.dumps({"error_description": "expired access/refresh token"}).encode("utf-8")
+
+ with (
+ unittest.mock.patch(
+ "posthog.temporal.data_imports.sources.salesforce.auth.make_tracked_session",
+ return_value=type("_S", (), {"post": staticmethod(lambda *a, **k: response)})(),
+ ),
+ pytest.raises(auth.SalesforceAuthRequestError) as exc,
+ ):
+ _ = auth.salesforce_refresh_access_token("something", "https://login.salesforce.com")
+
+ error_message = str(exc.value)
+ patterns = SalesforceSource().get_non_retryable_errors()
+ assert any(pattern in error_message for pattern in patterns), (
+ f"Salesforce auth error {error_message!r} did not match any non-retryable pattern"
+ )