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(batch-exports): fix handling of Snowflake invalid account errors (#64309)

Ross803ae01d4c8e
diff --git a/products/batch_exports/backend/api/destination_tests/snowflake.py b/products/batch_exports/backend/api/destination_tests/snowflake.py
index ee4f869a9579..e9c1056fa65e 100644
--- a/products/batch_exports/backend/api/destination_tests/snowflake.py
+++ b/products/batch_exports/backend/api/destination_tests/snowflake.py
@@ -81,7 +81,7 @@ class SnowflakeEstablishConnectionTestStep(DestinationTestStep):
async def _run_step(self) -> DestinationTestStepResult:
"""Run this test step."""
import snowflake.connector
- from snowflake.connector.errors import DatabaseError, InterfaceError, OperationalError
+ from snowflake.connector.errors import DatabaseError, HttpError, InterfaceError, OperationalError
private_key, result = try_load_private_key(self.private_key, self.private_key_passphrase)
if result is not None:
@@ -98,7 +98,7 @@ class SnowflakeEstablishConnectionTestStep(DestinationTestStep):
role=f'"{self.role}"' if self.role is not None else None,
session_parameters=_SNOWFLAKE_SESSION_PARAMETERS,
)
- except (OperationalError, InterfaceError, DatabaseError) as err:
+ except (OperationalError, InterfaceError, DatabaseError, HttpError) as err:
if err.msg is not None and "404 Not Found" in err.msg:
return DestinationTestStepResult(
status=Status.FAILED,
diff --git a/products/batch_exports/backend/temporal/destinations/snowflake_batch_export.py b/products/batch_exports/backend/temporal/destinations/snowflake_batch_export.py
index 3a30585cf950..1c5e16411127 100644
--- a/products/batch_exports/backend/temporal/destinations/snowflake_batch_export.py
+++ b/products/batch_exports/backend/temporal/destinations/snowflake_batch_export.py
@@ -19,7 +19,7 @@ from cryptography.hazmat.primitives import serialization
from snowflake.connector.connection import SnowflakeConnection
from snowflake.connector.constants import FIELD_ID_TO_NAME, QueryStatus
from snowflake.connector.cursor import ResultMetadata
-from snowflake.connector.errors import InterfaceError, OperationalError
+from snowflake.connector.errors import HttpError, InterfaceError, OperationalError
from structlog.contextvars import bind_contextvars
from temporalio import activity, workflow
from temporalio.common import RetryPolicy
@@ -605,6 +605,15 @@ class SnowflakeClient:
except InterfaceError as err:
raise SnowflakeConnectionError(f"Could not connect to Snowflake - {err.errno}: {err.msg}") from err
+ # Handle 404 errors (usually indicates the provided account is invalid)
+ except HttpError as err:
+ if err.msg is not None and "404 Not Found" in err.msg:
+ raise SnowflakeConnectionError(
+ f"Could not establish a connection to Snowflake as the resolved URL does not exist. This usually indicates an invalid Snowflake account."
+ ) from err
+ # allow other errors to raise in case they're temporary connection issues
+ raise
+
self.logger.debug("Connected to Snowflake")
self._connection = connection
diff --git a/products/batch_exports/backend/tests/api/destination_tests/test_snowflake_destination_tests.py b/products/batch_exports/backend/tests/api/destination_tests/test_snowflake_destination_tests.py
index b9d4739c98e6..70212003fed2 100644
--- a/products/batch_exports/backend/tests/api/destination_tests/test_snowflake_destination_tests.py
+++ b/products/batch_exports/backend/tests/api/destination_tests/test_snowflake_destination_tests.py
@@ -167,7 +167,11 @@ async def test_snowflake_establish_connection_with_wrong_user(snowflake_config):
assert result.status == Status.FAILED
assert result.message is not None
- assert "Incorrect username or password was specified." in result.message
+ # error depends on whether we're using password or keypair auth
+ if snowflake_config.get("password", None) is not None:
+ assert "Incorrect username or password was specified." in result.message
+ else:
+ assert "JWT token is invalid" in result.message
async def test_snowflake_establish_connection_with_invalid_private_key(snowflake_config):
@@ -307,7 +311,8 @@ def user(snowflake_cursor, password):
test_user = "EMPTY_TEST_USER"
snowflake_cursor.execute(f"DROP USER IF EXISTS {test_user}")
- snowflake_cursor.execute(f"CREATE USER {test_user} PASSWORD = '{password}'")
+ # Snowflake now enforces 2FA on many accounts so we need to bypass this for this new user
+ snowflake_cursor.execute(f"CREATE USER {test_user} PASSWORD = '{password}' MINS_TO_BYPASS_MFA = 30")
yield test_user
diff --git a/products/batch_exports/backend/tests/temporal/destinations/snowflake/conftest.py b/products/batch_exports/backend/tests/temporal/destinations/snowflake/conftest.py
index 3ffe2c67fa42..2174ea746610 100644
--- a/products/batch_exports/backend/tests/temporal/destinations/snowflake/conftest.py
+++ b/products/batch_exports/backend/tests/temporal/destinations/snowflake/conftest.py
@@ -113,6 +113,7 @@ def snowflake_cursor(snowflake_config: dict[str, str]):
role=f'"{snowflake_config["role"]}"' if snowflake_config["role"] is not None else None,
warehouse=f'"{snowflake_config["warehouse"]}"',
private_key=private_key,
+ login_timeout=5,
) as connection:
connection.telemetry_enabled = False
cursor = connection.cursor()