8000 wrap request exceptions in the library as well. · Twilio-api/twilio-python@2c5fa93 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c5fa93

Browse files
author
Kevin Burke
committed
wrap request exceptions in the library as well.
1 parent 9fd13ed commit 2c5fa93

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

twilio/rest/__init__.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,12 @@ def make_twilio_request(self, method, uri, **kwargs):
193193
:param dict params: the query parameters to attach to the request
194194
:param dict data: the POST form data to send with the request
195195
196-
:return: a requests-like HTTP response
197-
:rtype: :class:`RequestsResponse`
196+
:return: a response
197+
:rtype: :class:`requests.Response`
198198
: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.
200202
"""
201203
user_agent = "twilio-python/%s (Python %s)" % (
202204
twilio.__version__,
@@ -213,10 +215,14 @@ def make_twilio_request(self, method, uri, **kwargs):
213215

214216
uri += ".json"
215217

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+
220226
if not resp.ok:
221227
try:
222228
error = resp.json()
@@ -232,6 +238,21 @@ def make_twilio_request(self, method, uri, **kwargs):
232238
return resp
233239

234240

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+
235256
class TwilioRestException(TwilioException):
236257
""" A generic 400 or 500 level exception from the Twilio API
237258

0 commit comments

Comments
 (0)
0