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(clerk): stop retrying a 404 on the users list (#102182)

Tom Owers016911b1ac00
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.py
index 6ddf1eafd9bb..af1ad131f10c 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
@@ -242,6 +242,10 @@ _UNSUPPORTED_CHARACTER_MESSAGE = (
"Your Clerk secret key contains a character that can't be sent to Clerk, such as an invisible "
"one pasted from another app. Copy the key again from your Clerk dashboard and reconnect."
)
+INSTANCE_NOT_FOUND_MESSAGE = (
+ "Clerk can't find the data this secret key points at, so this table can't sync. Check the key "
+ "belongs to an active Clerk instance, then reconnect."
+)
def validate_credentials(secret_key: str) -> tuple[bool, str | None]:
@@ -273,6 +277,11 @@ def validate_credentials(secret_key: str) -> tuple[bool, str | None]:
return False, _INVALID_KEY_MESSAGE
if response.status_code == 403:
return False, _FORBIDDEN_KEY_MESSAGE
+ if response.status_code == 404:
+ # The users list exists on every Clerk instance, so a 404 here is Clerk refusing to resolve
+ # the instance the key belongs to rather than a missing record. That is user input like the
+ # statuses above, so explain it instead of filing an error.
+ return False, INSTANCE_NOT_FOUND_MESSAGE
# Any other status is unexpected for this endpoint; keep the raw detail for us instead of
# surfacing it to the user.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/source.py
index ba217e68debc..1aa0d2208165 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/source.py
@@ -8,6 +8,7 @@ from products.warehouse_sources.backend.facade.source_config import (
SourceFieldInputConfigType,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.clerk.clerk import (
+ INSTANCE_NOT_FOUND_MESSAGE,
ClerkResumeConfig,
clerk_source,
validate_credentials as validate_clerk_credentials,
@@ -135,6 +136,13 @@ The secret key starts with `sk_live_`.
# unfiltered list request is identical every run, so it re-fails on every schedule.
# Scoped to this path for the same reason as redirect_urls above.
"404 Client Error: Not Found for url: https://api.clerk.com/v1/jwt_templates": "The JWT templates table isn't available on your Clerk plan or instance. Turn off syncing for this table.",
+ # The users list exists on every Clerk instance, and an instance with no users answers
+ # 200 with an empty array, so a 404 resource_not_found here can't mean a missing record
+ # — Clerk won't resolve the instance the key belongs to at all. The request is identical
+ # every run, so it re-fails on every schedule. Scoped to this path, like the two
+ # entries above; `users` is also the parent of the sessions fan-out, so the copy names
+ # the key rather than one table.
+ "404 Client Error: Not Found for url: https://api.clerk.com/v1/users": INSTANCE_NOT_FOUND_MESSAGE,
**{reason: reason for reason in RETIRED_ENDPOINTS.values()},
}
diff --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.py
index 70d2f0a3900b..3e6998af0975 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
@@ -587,6 +587,7 @@ class TestClerkValidateCredentials:
(400, "invalid or has been revoked"),
(401, "invalid or has been revoked"),
(403, "does not have permission"),
+ (404, "active Clerk instance"),
(500, "Couldn't validate your Clerk secret key"),
],
)
@@ -608,6 +609,7 @@ class TestClerkValidateCredentials:
("status_code", "should_capture"),
[
(400, False), # malformed key is user input, not an error to file
+ (404, False), # an instance Clerk can't resolve is user input too
(500, True), # a genuine server fault still files an issue
],
)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk_source.py
index 73cadd69feec..0352f86f0d58 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk_source.py
@@ -39,6 +39,8 @@ class TestClerkSource:
"404 Client Error: Not Found for url: https://api.clerk.com/v1/redirect_urls?limit=100 | api error: code=resource_not_found",
# `jwt_templates` endpoint, not available on the account's Clerk plan or instance.
"404 Client Error: Not Found for url: https://api.clerk.com/v1/jwt_templates?limit=100 | api error: code=resource_not_found",
+ # `users` endpoint, which Clerk answers when it can't resolve the key's instance.
+ "404 Client Error: Not Found for url: https://api.clerk.com/v1/users?limit=100 | api error: code=resource_not_found",
],
)
def test_non_retryable_errors_matches_observed_error_message(self, observed_error):
@@ -66,9 +68,9 @@ class TestClerkSource:
assert not any(key in other_vendor_error for key in non_retryable_errors)
def test_non_retryable_errors_does_not_match_404_on_other_clerk_endpoints(self):
- # A 404 from a different endpoint may be a genuinely missing record worth investigating, not
- # the redirect_urls account limitation — the match must stay scoped to `redirect_urls`.
- other_endpoint_error = "404 Client Error: Not Found for url: https://api.clerk.com/v1/users?limit=100"
+ # A 404 from an endpoint we don't classify may be a genuinely missing record worth
+ # investigating — the matches must stay scoped to the paths where a 404 is deterministic.
+ other_endpoint_error = "404 Client Error: Not Found for url: https://api.clerk.com/v1/organizations?limit=100"
non_retryable_errors = self.source.get_non_retryable_errors()
assert not any(key in other_endpoint_error for key in non_retryable_errors)