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(cdp): log the refusal code when a transactional email is rejected (#125918)

Daniel Marchukdaf2237ab67c
diff --git a/products/messaging/backend/services/transactional_email.py b/products/messaging/backend/services/transactional_email.py
index 804975351759..b07ad141b48a 100644
--- a/products/messaging/backend/services/transactional_email.py
+++ b/products/messaging/backend/services/transactional_email.py
@@ -409,7 +409,8 @@ def send_transactional_email(
CDP_SEND_RESULTS.labels(result=_cdp_response_result(response.status_code)).inc()
if response.status_code != 202:
- refusal = PIPELINE_REFUSALS.get(_refusal_code(response) or "")
+ refusal_code = _refusal_code(response)
+ refusal = PIPELINE_REFUSALS.get(refusal_code or "")
if refusal is not None:
error_class, message = refusal
raise error_class(message)
@@ -418,6 +419,7 @@ def send_transactional_email(
team_id=team.id,
template_id=str(template_id),
status_code=response.status_code,
+ refusal_code=refusal_code,
)
raise DeliveryUnavailable()
diff --git a/products/messaging/backend/tests/api/test_transactional_email.py b/products/messaging/backend/tests/api/test_transactional_email.py
index 03ae73b8f81a..61a67aff020e 100644
--- a/products/messaging/backend/tests/api/test_transactional_email.py
+++ b/products/messaging/backend/tests/api/test_transactional_email.py
@@ -10,6 +10,7 @@ from parameterized import parameterized
from prometheus_client import REGISTRY
from rest_framework import status
from rest_framework.test import APIClient
+from structlog.testing import capture_logs
from posthog.constants import AvailableFeature
from posthog.models import OrganizationMembership, ProjectSecretAPIKey, Team, User
@@ -215,14 +216,20 @@ class TestTransactionalEmail(APIBaseTest):
@parameterized.expand(
[
- ("timeout", requests.ReadTimeout(), "timeout"),
- ("connect_timeout", requests.ConnectTimeout(), "timeout"),
- ("connection_error", requests.ConnectionError(), "connection_error"),
- ("cdp_api_error", MagicMock(status_code=500), "5xx"),
+ ("timeout", requests.ReadTimeout(), "timeout", None),
+ ("connect_timeout", requests.ConnectTimeout(), "timeout", None),
+ ("connection_error", requests.ConnectionError(), "connection_error", None),
+ ("cdp_api_error", MagicMock(status_code=500, json=dict), "5xx", None),
+ (
+ "invalid_request",
+ MagicMock(status_code=400, json=lambda: {"error": "invalid_request"}),
+ "4xx",
+ "invalid_request",
+ ),
]
)
def test_a_send_the_pipeline_does_not_confirm_is_unavailable(
- self, mock_post, _flag, _capture, _name, outcome, expected_result
+ self, mock_post, _flag, _capture, _name, outcome, expected_result, expected_refusal_code
):
if isinstance(outcome, Exception):
mock_post.side_effect = outcome
@@ -231,11 +238,15 @@ class TestTransactionalEmail(APIBaseTest):
metric = "messaging_transactional_email_cdp_requests_total"
before = REGISTRY.get_sample_value(metric, {"result": expected_result}) or 0
- response = self.client.post(self.url, self.body, format="json")
+ with capture_logs() as logs:
+ response = self.client.post(self.url, self.body, format="json")
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
assert response.json()["code"] == "delivery_unavailable"
assert REGISTRY.get_sample_value(metric, {"result": expected_result}) == before + 1
+ if not isinstance(outcome, Exception):
+ rejected = [log for log in logs if log["event"] == "transactional_email_queue_rejected"]
+ assert [log["refusal_code"] for log in rejected] == [expected_refusal_code]
@parameterized.expand(
[