From a76e8d483a48b0bb2b7b0bc7a632c2040d4cff57 Mon Sep 17 00:00:00 2001 From: Michael Wellman Date: Mon, 2 Dec 2024 07:30:38 -0500 Subject: [PATCH] Changed twilio exception handling to be more specific about max length issues --- app/clients/sms/twilio.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/app/clients/sms/twilio.py b/app/clients/sms/twilio.py index 1f0f961e20..65788f4f8e 100644 --- a/app/clients/sms/twilio.py +++ b/app/clients/sms/twilio.py @@ -22,6 +22,7 @@ RETRYABLE_STATUS_REASON, TWILIO_PROVIDER, ) +from app.exceptions import InvalidProviderException # https://www.twilio.com/docs/messaging/api/message-resource#message-status-values @@ -234,9 +235,17 @@ def send_sms( return message.sid except TwilioRestException as e: - self.logger.exception('Twilio send SMS request for %s failed', reference) - self.logger.debug('Twilio error code: %s, message: %s, details: %s', e.code, e.msg, e.details) - raise NonRetryableException('Twilio request failed') from e + 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 - %s: %s', e.code, e.msg) + raise NonRetryableException('Twilio request failed') from e + else: + raise except: self.logger.exception('Twilio send SMS request for %s failed', reference) raise