Skip to content

Commit

Permalink
jazzband#315 display a message in case of a Twilio exception
Browse files Browse the repository at this point in the history
if TWILIO_ERROR_MESSAGE is configured in settings, then an error message is added to the messages instance with this text.
  • Loading branch information
yoandyshyno committed Sep 30, 2019
1 parent 5a1018e commit b66fe18
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions two_factor/gateways/twilio/gateway.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

from django.conf import settings
from django.contrib import messages
from django.urls import reverse
from django.utils import translation
from django.utils.translation import pgettext, ugettext
Expand Down Expand Up @@ -64,10 +65,18 @@ def make_call(self, device, token):

def send_sms(self, device, token):
body = ugettext('Your authentication token is %s') % token
self.client.messages.create(
to=device.number.as_e164,
from_=getattr(settings, 'TWILIO_CALLER_ID'),
body=body)
try:
self.client.messages.create(
to=device.number.as_e164,
from_=getattr(settings, 'TWILIO_CALLER_ID'),
body=body)
except Exception:
twilio_error_message = getattr(settings, 'TWILIO_ERROR_MESSAGE', None)
if twilio_error_message:
request = get_current_request()
messages.add_message(request, messages.ERROR, twilio_error_message)
else:
raise


def validate_voice_locale(locale):
Expand Down

0 comments on commit b66fe18

Please sign in to comment.