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(warehouse-sources): explain a docs page the custom source builder cannot read (#107186)

Tom Owers63ae630a297b
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/ai_builder.py b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/ai_builder.py
index 6ce6af907b68..f610c8de0cc0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/ai_builder.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/ai_builder.py
@@ -129,6 +129,26 @@ def _html_to_text(html: str) -> str:
return re.sub(r"\n{3,}", "\n\n", "".join(parser.parts)).strip()
+# The builder's only other route is the "Configure manually" button, so every failure points there.
+_CONFIGURE_MANUALLY = 'choose "Configure manually"'
+
+
+def _docs_fetch_error_message(exc: requests.RequestException) -> str:
+ """Name the likely cause and a next step the builder actually offers.
+
+ `str(exc)` carries a status line and the URL the caller typed, which can hold a token, so the
+ raw reason is logged rather than returned.
+ """
+ status_code = exc.response.status_code if exc.response is not None else None
+ if status_code in (401, 403):
+ return f"That docs page needs a login. Link a page anyone can open, or {_CONFIGURE_MANUALLY}."
+ if status_code == 404:
+ return f"There is no page at that address. Check the link, or {_CONFIGURE_MANUALLY}."
+ if isinstance(exc, requests.Timeout):
+ return f"That docs page took too long to answer. Try again, or {_CONFIGURE_MANUALLY}."
+ return f"PostHog couldn't reach that docs page. Check the link, or {_CONFIGURE_MANUALLY}."
+
+
def fetch_docs_text(url: str) -> str:
"""Fetch an API docs page and return its text. HTML is reduced to visible text; other content
types are returned as-is. Relies on Smokescreen for SSRF protection — do NOT bypass the proxy.
@@ -147,14 +167,23 @@ def fetch_docs_text(url: str) -> str:
if len(raw) >= DOCS_FETCH_MAX_BYTES:
break
except requests.RequestException as exc:
- raise DocsFetchError(f"Could not fetch the docs URL: {exc}") from exc
+ # `str(exc)` embeds the URL, which can carry a token in its query string or userinfo, so the
+ # log gets the class and the status only.
+ logger.warning(
+ "custom_source_docs_fetch_failed",
+ error_type=type(exc).__name__,
+ status_code=exc.response.status_code if exc.response is not None else None,
+ )
+ raise DocsFetchError(_docs_fetch_error_message(exc)) from exc
text = raw.decode(response.encoding or "utf-8", errors="replace")
if "html" in content_type.lower():
text = _html_to_text(text)
text = text.strip()
if not text:
- raise DocsFetchError("The docs URL returned no readable text — paste the docs or an OpenAPI spec instead.")
+ raise DocsFetchError(
+ f"That docs page has no readable text. Link the API reference itself, or {_CONFIGURE_MANUALLY}."
+ )
return text
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_ai_builder.py b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_ai_builder.py
index 0c01cae8e2f0..306876aa3936 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_ai_builder.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/custom/test_ai_builder.py
@@ -3,10 +3,12 @@ from typing import cast
from django.test import SimpleTestCase
+import requests
from openai import OpenAI
from parameterized import parameterized
from products.warehouse_sources.backend.temporal.data_imports.sources.custom.ai_builder import (
+ _docs_fetch_error_message,
build_system_prompt,
build_user_prompt,
draft_manifest_sync,
@@ -160,3 +162,32 @@ class TestDraftManifestSync(SimpleTestCase):
)
self.assertEqual(result.status, "invalid")
self.assertIsNotNone(result.manifest_json)
+
+
+class TestDocsFetchErrorMessage(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("unauthorized", 401, "needs a login"),
+ ("forbidden", 403, "needs a login"),
+ ("not_found", 404, "no page at that address"),
+ ("server_error", 500, "couldn't reach"),
+ ]
+ )
+ def test_names_the_cause_without_leaking_the_request(self, _name: str, status_code: int, expected: str) -> None:
+ response = requests.Response()
+ response.status_code = status_code
+ response.url = "https://docs.example.com/api?token=sekrit"
+ exc = requests.HTTPError(f"{status_code} Client Error: for url: {response.url}", response=response)
+
+ message = _docs_fetch_error_message(exc)
+
+ self.assertIn(expected, message)
+ self.assertNotIn(str(status_code), message)
+ self.assertNotIn("docs.example.com", message)
+ self.assertNotIn("sekrit", message)
+
+ def test_timeout_says_to_try_again(self) -> None:
+ message = _docs_fetch_error_message(requests.Timeout("timed out"))
+
+ self.assertIn("too long to answer", message)
+ self.assertIn("Configure manually", message)