Skip to content

Report Connect Message send failures in messaging reports - #38091

Open
ajeety4 wants to merge 5 commits into
masterfrom
ay/connect-messaging-fixes
Open

Report Connect Message send failures in messaging reports#38091
ajeety4 wants to merge 5 commits into
masterfrom
ay/connect-messaging-fixes

Conversation

@ajeety4

@ajeety4 ajeety4 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Product Description

No user facing change.

Bug fixes to the Connect Message path: a send rejected by the Connect API is now recorded as an error on the messaging event instead of being reported as completed, and the Message Event Detail page renders Connect Message rows instead of erroring.

Technical Summary

https://dimagi.atlassian.net/browse/CI-910 - Observed while working on this ticket

Six issues in the Connect Message path:

  1. MessageEventDetailReport.rows dereferenced msg after ConnectMessage.DoesNotExist, and never initialised recipient.
    Fix: initialise the placeholders before the lookup, as the CONTENT_EMAIL branch does.
  2. ConnectBackend.send returned a bool that send_connect_message discarded, so a rejected send reported success — the subevent stayed IN_PROGRESS and the parent event was marked COMPLETED.
    Fix: the backend raises BackendProcessingException with the status code and body, send_connect_message errors the subevent, and ConnectMessageContent.send completes it.
  3. ConnectMessage.messaging_subevent was never set, so the detail report's lookup always missed — why 1 (above point 1) was reachable at all.
    Fix: pass get_sms_message_metadata(logged_subevent), as SMSContent does.
  4. log_sms_exception reads msg.couch_id, which ConnectMessage doesn't have, so real exceptions raised AttributeError instead of logging.
    Fix: fall back to message_id.
  5. Every send failure was labelled ERROR_CONNECT_GATEWAY, including a missing ConnectIDUserLink or absent couch user.
    Fix: get_connect_error_code maps by exception type; the backend wraps RequestException so network failures still count as gateway errors.
  6. ConnectMessageContent hardcoded case_id=None, losing the case association on its subevent.
    Fix: take it from self.case.

ConnectMessageSurveyContent needs no change — it sends via send_first_message, which already passes both metadata and logged_subevent.

Feature Flag

COMMCARE_CONNECT

Safety Assurance

Safety story

Low risk, scoped to the CONTENT_CONNECT path. No migrations.

ConnectBackend.send now raises where it returned False; its only caller already caught Exception, so nothing new escapes. ERROR_CONNECT_GATEWAY is a new value in a CharField with no choices constraint, so existing rows are unaffected.

Verified on a local dev domain against all four row shapes — delivered with and without a linked message, gateway 400 with and without one.

Automated test coverage

  • corehq/apps/reports/tests/test_message_event_detail_report.py — detail rows with and without a linked ConnectMessage; the second reproduces the UnboundLocalError.
  • corehq/messaging/scheduling/tests/test_content.py::TestConnectMessageContentSend — subevent completed on a 200, errored with the response body on a 400 and propagated to the parent event, message linked to the subevent, case recorded.
  • corehq/apps/sms/tests/test_api.pyget_connect_error_code over all three exception classes.

Each test was confirmed to fail on the unfixed code.

QA Plan

None

Rollback instructions

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

ajeety4 and others added 3 commits September 1, 2026 10:10
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Issue: ConnectBackend.send returned False for a non-200 response from the
Connect messaging API, send_connect_message discarded that return value,
and the subevent was never resolved. A rejected send was reported as
success, and the response body was lost.

Fix: raise from the backend with the status code and body, record it on
the subevent, and complete the subevent on success — the pattern
SMSContent and EmailContent already follow.

log_sms_exception assumed msg.couch_id, which ConnectMessage does not
have, so the existing error path raised AttributeError instead of logging.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
ConnectMessageContent.send passed no metadata, so add_msg_tags left
ConnectMessage.messaging_subevent NULL. The event detail report looks the
message up by that FK, so Content and ConnectID always rendered as '-'.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ajeety4 ajeety4 added the product/feature-flag Change will only affect users who have a specific feature flag enabled label Sep 1, 2026
ajeety4 and others added 2 commits September 3, 2026 13:52
send_connect_message labelled every exception ERROR_CONNECT_GATEWAY, so a
missing ConnectIDUserLink or an absent couch user was reported to the user
as a gateway problem. Map the exception to an error code instead.

The backend now also wraps requests.RequestException, so a network failure
still reports as a gateway error rather than an internal one.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
ConnectMessageContent.send hardcoded case_id=None, so a conditional alert
sending a Connect message lost the case association on its subevent.
Take it from self.case, as SMSContent and EmailContent do.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ajeety4
ajeety4 force-pushed the ay/connect-messaging-fixes branch from 4e6bc71 to 69bdb14 Compare September 3, 2026 08:23
@ajeety4
ajeety4 marked this pull request as ready for review September 3, 2026 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product/feature-flag Change will only affect users who have a specific feature flag enabled

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant