Skip to content

Commit 49827b0

Browse files
committed
if TWILIO_ERROR_MESSAGE is configured in settings, then an error message is added to the messages instance with this text.
1 parent 5a1018e commit 49827b0

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

two_factor/gateways/twilio/gateway.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22

33
from django.conf import settings
4+
from django.contrib import messages
45
from django.urls import reverse
56
from django.utils import translation
67
from django.utils.translation import pgettext, ugettext
@@ -38,9 +39,9 @@ class Twilio(object):
3839
``TWILIO_CALLER_ID``
3940
Should be set to a verified phone number. Twilio_ differentiates between
4041
numbers verified for making phone calls and sending text messages.
41-
42+
4243
Optionally you may set an error message to be displayed incase of error.
43-
44+
4445
``TWILIO_ERROR_MESSAGE``
4546
Should be set to a string with a custom text.
4647
@@ -64,10 +65,18 @@ def make_call(self, device, token):
6465

6566
def send_sms(self, device, token):
6667
body = ugettext('Your authentication token is %s') % token
67-
self.client.messages.create(
68-
to=device.number.as_e164,
69-
from_=getattr(settings, 'TWILIO_CALLER_ID'),
70-
body=body)
68+
try:
69+
self.client.messages.create(
70+
to=device.number.as_e164,
71+
from_=getattr(settings, 'TWILIO_CALLER_ID'),
72+
body=body)
73+
except Exception:
74+
twilio_error_message = getattr(settings, 'TWILIO_ERROR_MESSAGE', None)
75+
if twilio_error_message:
76+
request = get_current_request()
77+
messages.add_message(request, messages.ERROR, twilio_error_message)
78+
else:
79+
raise
7180

7281

7382
def validate_voice_locale(locale):

0 commit comments

Comments
 (0)