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(clay): tell the user what to change when clay rejects a table query (#108979)

Tom Owersec0f54d0f021
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clay/clay.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clay/clay.py
index b161783d4590..50c3da3cc7d7 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clay/clay.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clay/clay.py
@@ -118,6 +118,30 @@ def clay_source(
return SourceResponse(name=table_id, items=lambda: resource, primary_keys=None)
+_CLAY_REJECTED_KEY_MESSAGE = (
+ "Clay rejected PostHog's request while checking your API key. Create a new key under "
+ "Settings > Account > API keys in Clay, then reconnect."
+)
+
+_CLAY_REJECTED_TABLE_MESSAGE = (
+ "Clay rejected PostHog's request for one of your tables. Check that each value is a Clay table "
+ "ID or table URL, and that Enable for API is on in the table's settings."
+)
+
+_CLAY_UNAVAILABLE_MESSAGE = (
+ "Clay couldn't complete the request. Try again in a few minutes, and contact Clay support if it keeps failing."
+)
+
+
+def _unexpected_status_message(status_code: int, rejected_message: str) -> str:
+ """Copy for a status the checks above don't recognise.
+
+ A 429 or a 5xx clears on its own, so waiting is a real next step for both. Every other 4xx
+ fails the same way on every attempt, so it has to point at what the customer can change.
+ """
+ return _CLAY_UNAVAILABLE_MESSAGE if status_code == 429 or status_code >= 500 else rejected_message
+
+
def validate_credentials(api_key: str, table_ids: list[str]) -> tuple[bool, str | None]:
if not table_ids:
return False, "Enter at least one Clay table ID or table URL."
@@ -128,10 +152,7 @@ def validate_credentials(api_key: str, table_ids: list[str]) -> tuple[bool, str
if me.status_code == 401:
return False, "Clay rejected the API key. Create a new key under Settings > Account > API keys in Clay."
if me.status_code != 200:
- return (
- False,
- f"Clay returned an unexpected status ({me.status_code}) while checking the API key. Try again in a few minutes.",
- )
+ return False, _unexpected_status_message(me.status_code, _CLAY_REJECTED_KEY_MESSAGE)
for table_id in table_ids:
res = session.post(
@@ -147,9 +168,6 @@ def validate_credentials(api_key: str, table_ids: list[str]) -> tuple[bool, str
)
if res.status_code == 404:
return False, f"Clay could not find table {table_id}. Check the table ID or URL."
- return (
- False,
- f"Clay returned an unexpected status ({res.status_code}) for table {table_id}. Try again in a few minutes.",
- )
+ return False, _unexpected_status_message(res.status_code, _CLAY_REJECTED_TABLE_MESSAGE)
return True, None
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clay/tests/test_clay.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clay/tests/test_clay.py
index 3a179bdebfd2..358776ddb6a0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clay/tests/test_clay.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clay/tests/test_clay.py
@@ -133,10 +133,17 @@ class TestClaySourceBehavior:
[
(200, 200, True, None),
(401, None, False, "rejected the API key"),
- (500, None, False, "unexpected status (500)"),
+ (403, None, False, "while checking your API key"),
+ # 429 and 5xx clear on their own, so waiting is a real next step. Every other 4xx fails
+ # the same way on every attempt, so the copy names what the customer can change instead.
+ (429, None, False, "Try again in a few minutes"),
+ (500, None, False, "Try again in a few minutes"),
(200, 403, False, "Enterprise plan"),
(200, 404, False, "could not find table"),
- (200, 422, False, "unexpected status (422)"),
+ (200, 400, False, "Enable for API"),
+ (200, 422, False, "Enable for API"),
+ (200, 429, False, "Try again in a few minutes"),
+ (200, 503, False, "Try again in a few minutes"),
],
)
def test_validate_credentials(
@@ -158,6 +165,24 @@ def test_validate_credentials(
assert message is not None and expected_message_part in message
+def test_validate_credentials_does_not_echo_the_table_id_for_an_unrecognised_status() -> None:
+ # The message for an unrecognised status must carry neither the submitted table ID nor the
+ # status code: one is a value the customer already has, the other is not actionable.
+ with patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.clay.clay.make_tracked_session"
+ ) as MockSession:
+ session = MockSession.return_value
+ session.get.return_value = _make_http_response({}, 200)
+ session.post.return_value = _make_http_response({}, 400)
+
+ valid, message = validate_credentials("clay_test_key", [TABLE_ID])
+
+ assert valid is False
+ assert message is not None
+ assert TABLE_ID not in message
+ assert "400" not in message
+
+
def test_validate_credentials_requires_a_table_id() -> None:
with patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.clay.clay.make_tracked_session"