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(algolia): actionable error when validation returns 404 (#70984)

Tom Owers7470175b47b8
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/algolia/algolia.py b/products/warehouse_sources/backend/temporal/data_imports/sources/algolia/algolia.py
index 7707c1257448..f1e3d232e1b9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/algolia/algolia.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/algolia/algolia.py
@@ -259,7 +259,31 @@ def validate_credentials(
return True, None
return False, f"Your Algolia API key is missing the ACL required to sync '{schema_name}'"
- return False, f"Algolia API returned status {response.status_code}"
+ # Any other status is unexpected for a credential probe. Surface Algolia's own message when it
+ # sends one, and give the common 404 an actionable hint, rather than echoing a bare status code
+ # the user can't act on.
+ api_message = ""
+ try:
+ api_message = response.json().get("message", "")
+ except ValueError:
+ pass
+
+ if response.status_code == 404:
+ target = f"index '{index_name}'" if index_name else "requested resource"
+ detail = f" Algolia said: {api_message}." if api_message else ""
+ return (
+ False,
+ f"Algolia couldn't find the {target} (status 404). Check that your Application ID is "
+ f"correct and the index exists, then try again.{detail}",
+ )
+
+ if api_message:
+ return False, f"Algolia rejected the request (status {response.status_code}): {api_message}"
+ return (
+ False,
+ f"Algolia returned an unexpected status ({response.status_code}). Check your Application ID "
+ "and API key, then try again.",
+ )
def algolia_source(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/algolia/tests/test_algolia.py b/products/warehouse_sources/backend/temporal/data_imports/sources/algolia/tests/test_algolia.py
index c7c4ca3c83e3..5230e5e12de5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/algolia/tests/test_algolia.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/algolia/tests/test_algolia.py
@@ -299,3 +299,29 @@ class TestValidateCredentials:
valid, error = self._run(_response({}, status_code=500), index_name="idx")
assert valid is False
assert error is not None and "500" in error
+
+ def test_not_found_returns_actionable_message(self) -> None:
+ # A 404 means the probed index (or application ID) doesn't exist. Give the user something to
+ # act on instead of a bare "returned status 404".
+ resp = _response({"message": "Index does not exist"}, status_code=404)
+ valid, error = self._run(resp, index_name="idx")
+ assert valid is False
+ assert error is not None
+ assert "Application ID" in error and "index" in error
+
+ def test_unexpected_status_surfaces_algolia_message(self) -> None:
+ # A non-404 error carrying an Algolia message should surface it so the user isn't left with
+ # only a status code.
+ resp = _response({"message": "Request rejected"}, status_code=400)
+ valid, error = self._run(resp, index_name="idx")
+ assert valid is False
+ assert error is not None and "Request rejected" in error
+
+ def test_non_json_error_body_does_not_crash(self) -> None:
+ # A non-JSON error body (e.g. an HTML 502 page) must not blow up validation; fall back to a
+ # status-based message.
+ resp = _response({}, status_code=502)
+ resp.json.side_effect = ValueError("no json")
+ valid, error = self._run(resp, index_name="idx")
+ assert valid is False
+ assert error is not None and "502" in error