@@ -193,10 +193,12 @@ def make_twilio_request(self, method, uri, **kwargs):
193
193
:param dict params: the query parameters to attach to the request
194
194
:param dict data: the POST form data to send with the request
195
195
196
- :return: a requests-like HTTP response
197
- :rtype: :class:`RequestsResponse `
196
+ :return: a response
197
+ :rtype: :class:`requests.Response `
198
198
:raises TwilioRestException: if the response is a 400
199
- or 500-level response, or a timeout.
199
+ or 500-level response
200
+ :raises TwilioRequestException: if a response was not received; most
201
+ likely a timeout, but possibly a connection error.
200
202
"""
201
203
user_agent = "twilio-python/%s (Python %s)" % (
202
204
twilio .__version__ ,
@@ -213,10 +215,14 @@ def make_twilio_request(self, method, uri, **kwargs):
213
215
214
216
uri += ".json"
215
217
216
- resp = requests .request (method , uri , auth = self .auth , headers = headers ,
217
- timeout = self .transport .timeout ,
218
- proxies = self .transport .proxies ,
219
- ** kwargs )
218
+ try :
219
+ resp = requests .request (method , uri , auth = self .auth ,
220
+ headers = headers ,
221
+ timeout = self .transport .timeout ,
222
+ proxies = self .transport .proxies , ** kwargs )
223
+ except requests .RequestException as e :
224
+ raise TwilioRequestException (e )
225
+
220
226
if not resp .ok :
221
227
try :
222
228
error = resp .json ()
@@ -232,6 +238,21 @@ def make_twilio_request(self, method, uri, **kwargs):
232
238
return resp
233
239
234
240
241
+ class TwilioRequestException (TwilioException ):
242
+ """ An exception raised when we don't get a HTTP response from the server
243
+
244
+ :param Exception reason: The wrapped exception. see requests/exceptions.py
245
+ for more information.
246
+ :param str url: the requested URL that raised an exception.
247
+ """
248
+ def __init__ (self , url , reason ):
249
+ self .reason = reason
250
+ message = ("Exception caused when requesting url: "
251
+ "{url} (Caused by {typ}: {e})" .format (
252
+ url = url , typ = type (reason ), e = reason ))
253
+ TwilioException .__init__ (self , message )
254
+
255
+
235
256
class TwilioRestException (TwilioException ):
236
257
""" A generic 400 or 500 level exception from the Twilio API
237
258
0 commit comments