: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(warehouse): explain a rejected Clerk key on 400 (#91407)
posthog[bot]eb28954ae707
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.pyindex 110050f85e11..36c0725a979b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py@@ -256,7 +256,10 @@ def validate_credentials(secret_key: str) -> tuple[bool, str | None]:if response.status_code == 200:return True, None- if response.status_code == 401:+ if response.status_code in (400, 401):+ # Our request (GET /v1/users?limit=1) is always well-formed, so a 400 is Clerk rejecting the+ # credential itself — a malformed or wrong-format secret key. That is user input, like a 401+ # revoked key, so explain it the same way instead of filing an error.return False, _INVALID_KEY_MESSAGEif response.status_code == 403:return False, _FORBIDDEN_KEY_MESSAGEdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.pyindex 4140eabd8b2c..9f782b664669 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py@@ -574,13 +574,15 @@ class TestClerkRetiredEndpoints:)-_VALIDATE_SESSION = "products.warehouse_sources.backend.temporal.data_imports.sources.clerk.clerk.make_tracked_session"+_CLERK_MODULE = "products.warehouse_sources.backend.temporal.data_imports.sources.clerk.clerk"+_VALIDATE_SESSION = f"{_CLERK_MODULE}.make_tracked_session"class TestClerkValidateCredentials:@pytest.mark.parametrize(("status_code", "expected_substring"),[+ (400, "invalid or has been revoked"),(401, "invalid or has been revoked"),(403, "does not have permission"),(500, "Couldn't validate your Clerk secret key"),@@ -600,6 +602,23 @@ class TestClerkValidateCredentials:assert expected_substring in (message or "")assert sentinel not in (message or "")+ @pytest.mark.parametrize(+ ("status_code", "should_capture"),+ [+ (400, False), # malformed key is user input, not an error to file+ (500, True), # a genuine server fault still files an issue+ ],+ )+ def test_only_server_faults_file_an_error(self, status_code: int, should_capture: bool) -> None:+ response = _make_http_response({"errors": [{"code": "bad"}]}, status_code=status_code)+ with (+ patch(_VALIDATE_SESSION) as mock_session,+ patch(f"{_CLERK_MODULE}.capture_exception") as mock_capture,+ ):+ mock_session.return_value.get.return_value = response+ validate_credentials("sk_test_key")+ assert mock_capture.called is should_capture+def test_network_error_returns_actionable_message_without_leaking_exception(self) -> None:with patch(_VALIDATE_SESSION) as mock_session:mock_session.return_value.get.side_effect = RequestException("connection reset by peer")