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.

feat(messaging): point a sender refusal at the from a send names (#127181)

Michael Reichenbach23f3570b9dbd
diff --git a/products/messaging/backend/facade/contracts.py b/products/messaging/backend/facade/contracts.py
index 6416bbfc6e3f..e9d1214523aa 100644
--- a/products/messaging/backend/facade/contracts.py
+++ b/products/messaging/backend/facade/contracts.py
@@ -38,6 +38,7 @@ class TransactionalEmailError(Exception):
"""A send the caller has to fix before retrying. `code` is stable for API clients."""
code = "invalid"
+ attr: str | None = None
class TemplateNotSendable(TransactionalEmailError):
@@ -102,6 +103,10 @@ class SenderNotVerified(TransactionalEmailError):
code = "sender_not_verified"
+class NamedSenderNotVerified(SenderNotVerified):
+ attr = "from"
+
+
class SenderRequired(TransactionalEmailError):
code = "sender_required"
diff --git a/products/messaging/backend/presentation/views/message_templates.py b/products/messaging/backend/presentation/views/message_templates.py
index ca0536b69874..7102afea3b03 100644
--- a/products/messaging/backend/presentation/views/message_templates.py
+++ b/products/messaging/backend/presentation/views/message_templates.py
@@ -597,7 +597,7 @@ class SendTransactionalEmailResponseSerializer(serializers.Serializer):
)
-class TransactionalEmailRejected(exceptions.APIException):
+class TransactionalEmailRejected(exceptions.ValidationError):
STATUS_BY_CODE = {
"quota_limited": 429,
"idempotency_key_in_flight": 409,
@@ -607,12 +607,12 @@ class TransactionalEmailRejected(exceptions.APIException):
"recipient_suppressed": 422,
"recipient_unreachable": 422,
}
- # The caller has to change the request. Without this the error handler reports a server error.
+ # The caller has to change the request. Without this the error handler types it `validation_error`.
default_type = "invalid_request"
def __init__(self, error: TransactionalEmailError) -> None:
self.status_code = self.STATUS_BY_CODE.get(error.code, status.HTTP_400_BAD_REQUEST)
- super().__init__(detail=str(error), code=error.code)
+ super().__init__(detail={error.attr: [str(error)]} if error.attr else str(error), code=error.code)
class TransactionalEmailUnavailable(exceptions.APIException):
diff --git a/products/messaging/backend/services/transactional_email.py b/products/messaging/backend/services/transactional_email.py
index f38f9c8abe78..410aa3117f54 100644
--- a/products/messaging/backend/services/transactional_email.py
+++ b/products/messaging/backend/services/transactional_email.py
@@ -32,6 +32,7 @@ from products.messaging.backend.facade.contracts import (
InvalidRecipient,
InvalidRequest,
MarketingTemplate,
+ NamedSenderNotVerified,
QueuedEmail,
QuotaLimited,
RecipientSuppressed,
@@ -557,7 +558,7 @@ def _code_send_sender(team_id: int, address: str | None) -> dict[str, Any]:
) from e
except TransactionalEmailSenderUnavailable as e:
if address:
- raise SenderNotVerified(f"No verified sender in this project can send from `{email}`.") from e
+ raise NamedSenderNotVerified(f"No verified sender in this project can send from `{email}`.") from e
raise SenderNotVerified("This project has no verified email sender. Verify a sender domain first.") from e
return {"integrationId": sender_id, "email": email, "name": name}
diff --git a/products/messaging/backend/tests/api/test_transactional_email.py b/products/messaging/backend/tests/api/test_transactional_email.py
index 8040bc32d368..7c8b9233fdae 100644
--- a/products/messaging/backend/tests/api/test_transactional_email.py
+++ b/products/messaging/backend/tests/api/test_transactional_email.py
@@ -736,7 +736,11 @@ class TestTransactionalEmail(APIBaseTest):
if code is None:
assert json.loads(mock_post.call_args.kwargs["data"])["send"]["from"] == {"integrationId": created[0].id}
else:
- assert response.json()["code"] == code
+ assert (response.json()["type"], response.json()["code"], response.json()["attr"]) == (
+ "invalid_request",
+ code,
+ None,
+ )
mock_post.assert_not_called()
def test_refuses_a_code_send_from_an_unverified_domain(self, mock_post, _flag, capture):
@@ -745,7 +749,11 @@ class TestTransactionalEmail(APIBaseTest):
)
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()
- assert response.json()["code"] == "sender_not_verified"
+ assert (response.json()["type"], response.json()["code"], response.json()["attr"]) == (
+ "invalid_request",
+ "sender_not_verified",
+ "from",
+ )
assert not TransactionalEmailKey.objects.for_team(self.team.id, canonical=True).exists()
assert capture.call_args.kwargs["properties"]["error_code"] == "sender_not_verified"
mock_post.assert_not_called()