diff --git a/app/clients/sms/twilio.py b/app/clients/sms/twilio.py index 847171861d..81f57e873e 100644 --- a/app/clients/sms/twilio.py +++ b/app/clients/sms/twilio.py @@ -176,7 +176,6 @@ def send_sms( Return: a string containing the Twilio message.sid """ - start_time = monotonic() from app.dao.service_sms_sender_dao import ( dao_get_service_sms_sender_by_service_id_and_number, @@ -239,12 +238,12 @@ def send_sms( if e.status == 400 and 'phone number' in e.msg: self.logger.exception('Twilio send SMS request for %s failed', reference) raise InvalidProviderException from e - # elif e.status == 400 and e.code == 21617: # Twilio error code for max length exceeded - # self.logger.exception( - # 'Twilio send SMS request for %s failed, message content max length exceeded.', reference - # ) - # self.logger.debug('Twilio error details for %s - %s: %s', reference, e.code, e.msg) - # raise NonRetryableException('Twilio request failed') from e + elif e.status == 400 and e.code == 21617: # Twilio error code for max length exceeded + self.logger.exception( + 'Twilio send SMS request for %s failed, message content max length exceeded.', reference + ) + self.logger.debug('Twilio error details for %s - %s: %s', reference, e.code, e.msg) + raise NonRetryableException('Twilio request failed') from e else: raise except: diff --git a/tests/app/clients/test_twilio.py b/tests/app/clients/test_twilio.py index 6544efe60d..79a97bda5d 100644 --- a/tests/app/clients/test_twilio.py +++ b/tests/app/clients/test_twilio.py @@ -599,19 +599,43 @@ def test_send_sms_twilio_callback( assert response_dict['sid'] == twilio_sid +@pytest.mark.parametrize( + ( + 'response_dict', + 'exception', + ), + ( + ( + { + 'code': 21606, + 'message': "The 'From' phone number provided (+61412345678) is not a valid message-capable Twilio phone number for this destination", + }, + InvalidProviderException, + ), + ( + { + 'code': 21617, + 'message': 'Unable to create record: The concatenated message body exceeds the 1600 character limit', + }, + NonRetryableException, + ), + ), + ids=( + 'invalid-from-number', + 'message-too-long', + ), +) def test_send_sms_raises_invalid_provider_error_with_invalid_twilio_number( notify_api, mocker, + response_dict, + exception, ): to = '+61412345678' content = 'my message' reference = 'my reference' - response_dict = { - 'code': 21606, - 'message': "The 'From' phone number provided (+61412345678) is not a valid message-capable Twilio phone number for this destination", - } - with pytest.raises(InvalidProviderException): + with pytest.raises(exception): with requests_mock.Mocker() as r_mock: r_mock.post( f'https://api.twilio.com/2010-04-01/Accounts/{twilio_sms_client._account_sid}/Messages.json',