: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(proxy): return actionable error when reverse proxy diagnose fails (#59725)
Fernando Gomes94440917dded
diff --git a/posthog/api/proxy_record.py b/posthog/api/proxy_record.pyindex 49ef1b88fa59..a9bf6418fd08 100644--- a/posthog/api/proxy_record.py+++ b/posthog/api/proxy_record.py@@ -306,7 +306,20 @@ class ProxyRecordViewset(TeamAndOrgViewSetMixin, ModelViewSet):status=status.HTTP_429_TOO_MANY_REQUESTS,)- report = diagnose_proxy_record(record)+ try:+ report = diagnose_proxy_record(record)+ except Exception as e:+ capture_exception(e, {"proxy_record_id": str(record.id), "domain": record.domain})+ return Response(+ {+ "detail": (+ "Couldn't run diagnostics for this proxy. Please try again in a few minutes; "+ "if this keeps happening, contact PostHog support."+ )+ },+ status=status.HTTP_500_INTERNAL_SERVER_ERROR,+ )+serializer = DiagnosticReportSerializer(report)return Response(serializer.data)diff --git a/posthog/api/proxy_record_diagnostics.py b/posthog/api/proxy_record_diagnostics.pyindex 95bd2421ff42..be2b8427c025 100644--- a/posthog/api/proxy_record_diagnostics.py+++ b/posthog/api/proxy_record_diagnostics.py@@ -295,6 +295,26 @@ def _check_cloudflare(record: ProxyRecord) -> tuple[CheckResult, Optional[Custom),None,)+ except Exception as e:+ # Catches ValueError from misconfigured CLOUDFLARE_API_TOKEN/ZONE_ID,+ # ValueError/KeyError from response shape changes in _parse_hostname,+ # and any other unexpected library exception. "warned" (not "failed") so+ # the rest of the report still runs — downstream checks already _skip+ # when hostname_info is None.+ capture_exception(e, {"proxy_record_id": str(record.id), "domain": record.domain})+ return (+ CheckResult(+ id="cloudflare",+ name="Cloudflare custom hostname",+ status="warned",+ detail=(+ "We couldn't query our certificate provider for this proxy's status. "+ "Other checks below may still be informative. Try again in a few minutes; "+ "if it keeps happening, contact support."+ ),+ ),+ None,+ )if info is None:return (diff --git a/posthog/api/test/test_proxy_record.py b/posthog/api/test/test_proxy_record.pyindex 6edbeb291eb0..db5ee55c9a17 100644--- a/posthog/api/test/test_proxy_record.py+++ b/posthog/api/test/test_proxy_record.py@@ -502,3 +502,33 @@ class TestProxyRecordAPI(APIBaseTest):# Sanity: diagnose function only called once despite two requests.assert mock_diagnose.call_count == 1++ @patch("posthog.api.proxy_record.diagnose_proxy_record")+ def test_diagnose_returns_structured_error_on_unexpected_exception(self, mock_diagnose):+ from django.core.cache import cache++ cache.clear()+ mock_diagnose.side_effect = RuntimeError("something broke")+ record = ProxyRecord.objects.create(+ organization=self.organization,+ created_by=self.user,+ domain="failed-diagnose.example.com",+ target_cname="abc123.proxy.posthog.com",+ status=ProxyRecord.Status.ERRORING,+ )++ with patch("posthog.api.proxy_record.capture_exception") as cap_mock:+ response = self.client.post(+ f"/api/organizations/{self.organization.id}/proxy_records/{record.id}/diagnose/",+ )++ assert response.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR+ body = response.json()+ # Regression-pin: must not be DRF's default 500 message.+ assert body["detail"] != "A server error occurred."+ assert "try again" in body["detail"].lower()+ assert "support" in body["detail"].lower()+ cap_mock.assert_called_once()+ _exc, props = cap_mock.call_args[0]+ assert props["proxy_record_id"] == str(record.id)+ assert props["domain"] == record.domaindiff --git a/posthog/api/test/test_proxy_record_diagnostics.py b/posthog/api/test/test_proxy_record_diagnostics.pyindex 500e93953d4e..7f737c72ae1f 100644--- a/posthog/api/test/test_proxy_record_diagnostics.py+++ b/posthog/api/test/test_proxy_record_diagnostics.py@@ -127,6 +127,30 @@ class TestCheckCloudflare(TestCase):self.assertEqual(result.status, "failed")self.assertIn("certificate provider", result.detail.lower())+ @parameterized.expand(+ [+ # ValueError from cloudflare._get_headers() when CLOUDFLARE_API_TOKEN is unset,+ # or from cloudflare._parse_hostname() when Cloudflare returns an unknown enum+ # status value.+ ("value_error", ValueError("CLOUDFLARE_API_TOKEN must be configured")),+ # KeyError from cloudflare._parse_hostname() if the response is missing a+ # previously-guaranteed field like `id`, `hostname`, or `status`.+ ("key_error", KeyError("status")),+ ]+ )+ @patch("posthog.api.proxy_record_diagnostics.get_custom_hostname_by_domain")+ def test_warn_when_unexpected_exception_escapes(self, _name, exc, get_mock):+ get_mock.side_effect = exc+ with patch("posthog.api.proxy_record_diagnostics.capture_exception") as cap_mock:+ result, info = diagnostics._check_cloudflare(_record())+ self.assertEqual(result.status, "warned")+ self.assertIsNone(info)+ self.assertIsNone(result.remediation)+ cap_mock.assert_called_once()+ _exc, props = cap_mock.call_args[0]+ self.assertIn("proxy_record_id", props)+ self.assertIn("domain", props)+class TestCheckCaa(TestCase):@patch("posthog.api.proxy_record_diagnostics.dns.resolver.Resolver")