: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(data-warehouse): match MongoDB auth failure casing in non-retryable errors (#61610)
Tom Owerseca36f27822d
diff --git a/posthog/temporal/data_imports/sources/mongodb/source.py b/posthog/temporal/data_imports/sources/mongodb/source.pyindex d68a3ea2df90..521de8567635 100644--- a/posthog/temporal/data_imports/sources/mongodb/source.py+++ b/posthog/temporal/data_imports/sources/mongodb/source.py@@ -26,6 +26,11 @@ from posthog.temporal.data_imports.sources.mongodb.mongo import (from products.data_warehouse.backend.types import ExternalDataSourceType+_MONGO_UNREACHABLE_MESSAGE = (+ "Could not reach your MongoDB cluster. Check that the cluster is running and that PostHog's "+ "IP addresses are allowlisted in your database's network access settings."+)+@SourceRegistry.registerclass MongoDBSource(SimpleSource[MongoDBSourceConfig], ValidateDatabaseHostMixin):@@ -34,7 +39,24 @@ class MongoDBSource(SimpleSource[MongoDBSourceConfig], ValidateDatabaseHostMixinreturn ExternalDataSourceType.MONGODBdef get_non_retryable_errors(self) -> dict[str, str | None]:- return {"The DNS query name does not exist": None, "authentication failed": None, "SSL handshake failed": None}+ auth_failed_msg = "MongoDB authentication failed. Please check the username and password for this source."+ return {+ "The DNS query name does not exist": None,+ # pymongo raises OperationFailure with codeName 'AuthenticationFailed' (code 18) and+ # errmsg 'Authentication failed.' when the credentials are wrong. Non-retryable error+ # matching is case-sensitive, so the previous lowercase "authentication failed" never+ # matched the real message — key off the stable codeName and the capitalised message.+ "AuthenticationFailed": auth_failed_msg,+ "Authentication failed": auth_failed_msg,+ "SSL handshake failed": None,+ # pymongo raises ServerSelectionTimeoutError ("No servers found yet" / "No replica set+ # members found yet") when it can't reach any cluster node for the whole selection+ # timeout. On a managed cluster this is a persistent connectivity problem — the worker+ # IP isn't allowlisted, or the cluster is paused/decommissioned — not a momentary blip+ # (a mid-sync outage surfaces differently), so retrying the job won't recover it.+ "No servers found yet": _MONGO_UNREACHABLE_MESSAGE,+ "No replica set members found yet": _MONGO_UNREACHABLE_MESSAGE,+ }def get_schemas(self,diff --git a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py b/posthog/temporal/data_imports/sources/mongodb/test_mongo.pyindex 57a4ba741159..b8d0abd90027 100644--- a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py+++ b/posthog/temporal/data_imports/sources/mongodb/test_mongo.py@@ -316,3 +316,61 @@ class TestGetLeadingIndexKeys(SimpleTestCase):def test_returns_empty_set_for_collection_with_no_indexes(self):coll = self._collection_with_indexes([])assert get_leading_index_keys(coll) == set()+++class TestMongoDBNonRetryableErrors(SimpleTestCase):+ """The non-retryable match is case-sensitive substring matching, so the patterns+ must match the exact casing pymongo produces."""++ def setUp(self):+ from posthog.temporal.data_imports.sources.mongodb.source import MongoDBSource++ self.non_retryable = MongoDBSource().get_non_retryable_errors()++ @parameterized.expand(+ [+ # Real pymongo OperationFailure string for bad credentials (code 18).+ (+ "operation_failure",+ "Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', "+ "'code': 18, 'codeName': 'AuthenticationFailed'}",+ ),+ ("dns_failure", "The DNS query name does not exist: example.mongodb.net."),+ ("ssl_failure", "SSL handshake failed: certificate verify failed"),+ # Cluster unreachable for the whole selection timeout — persistent connectivity issue.+ ("no_servers", "No servers found yet, Timeout: 5.0s, Topology Description: ..."),+ (+ "no_replica_set_members",+ "No replica set members found yet, Timeout: 10.0s, Topology Description: "+ "<TopologyDescription topology_type: ReplicaSetNoPrimary>",+ ),+ ]+ )+ def test_known_errors_are_non_retryable(self, _name, error_msg):+ assert any(pattern in error_msg for pattern in self.non_retryable), (+ f"MongoDB error {error_msg!r} did not match any non-retryable pattern"+ )++ @parameterized.expand(+ [+ ("connection_reset", "connection closed"),+ ("network_timeout", "NetworkTimeout: timed out reading from socket"),+ ]+ )+ def test_transient_errors_are_retryable(self, _name, error_msg):+ assert not any(pattern in error_msg for pattern in self.non_retryable), (+ f"MongoDB error {error_msg!r} should remain retryable"+ )++ @parameterized.expand(+ [+ ("code_name", "AuthenticationFailed", "password"),+ ("message", "Authentication failed", "password"),+ ("unreachable_no_servers", "No servers found yet", "allowlist"),+ ("unreachable_no_replica_set", "No replica set members found yet", "allowlist"),+ ]+ )+ def test_pattern_has_friendly_message(self, _name, pattern, expected_substring):+ message = self.non_retryable[pattern]+ assert message is not None+ assert expected_substring in message.lower()