Skip to content

Commit

Permalink
Changed twilio exception handling to be more specific about max lengt…
Browse files Browse the repository at this point in the history
…h issues
  • Loading branch information
mchlwellman committed Dec 2, 2024
1 parent 413cbde commit a76e8d4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions app/clients/sms/twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a76e8d4

Please sign in to comment.