fix: Error shouldn't halt the campaign for entire audience (#11980)

## Summary
- handle Twilio failures per contact when running one-off SMS campaigns
- rescue errors in WhatsApp and generic SMS one-off campaigns so they
continue
- add specs confirming campaigns continue sending when a single contact
fails

fixes:  https://github.com/chatwoot/chatwoot/issues/9000

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Sojan Jose
2025-08-10 23:33:48 -07:00
committed by GitHub
parent 8ea21eeefd
commit 3214d06a83
6 changed files with 70 additions and 8 deletions

View File

@@ -20,6 +20,7 @@ describe Sms::OneoffSmsCampaignService do
body: { 'id' => '1' }.to_json,
headers: {}
)
allow_any_instance_of(described_class).to receive(:channel).and_return(sms_channel) # rubocop:disable RSpec/AnyInstance
end
it 'raises error if the campaign is completed' do
@@ -52,5 +53,21 @@ describe Sms::OneoffSmsCampaignService do
sms_campaign_service.perform
end
it 'continues processing contacts when sending message raises an error' do
contact_error, contact_success = FactoryBot.create_list(:contact, 2, :with_phone_number, account: account)
contact_error.update_labels([label1.title])
contact_success.update_labels([label1.title])
error_message = 'SMS provider error'
expect(sms_channel).to receive(:send_text_message).with(contact_error.phone_number, anything).and_raise(StandardError, error_message)
expect(sms_channel).to receive(:send_text_message).with(contact_success.phone_number, anything).and_return(nil)
expect(Rails.logger).to receive(:error).with("[SMS Campaign #{campaign.id}] Failed to send to #{contact_error.phone_number}: #{error_message}")
sms_campaign_service.perform
expect(campaign.reload.completed?).to be true
end
end
end