8000 feat: add details to TwilioRestException (#517) · dialex-com/twilio-python@1a0d02d · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a0d02d

Browse files
authored
feat: add details to TwilioRestException (twilio#517)
1 parent 5462b05 commit 1a0d02d

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

tests/unit/http/test_http_client.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from mock import patch, Mock
55
from requests import Session
66

7+
from twilio.base.version import Version
8+
from twilio.base.exceptions import TwilioRestException
79
from twilio.http.http_client import TwilioHttpClient
810
from twilio.http.response import Response
911

@@ -64,7 +66,7 @@ def test_request_where_class_timeout_manually_set(self):
6466
self.client.timeout = 30
6567

6668
response = self.client.request(
67-
'doesnt matter', 'doesnt matter')
69+
'doesnt matter', 'doesnt matter')
6870
self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
6971
self.assertEqual(200, response.status_code)
7072
self.assertEqual('testing-unicode: Ω≈ç√, 💩', response.content)
@@ -143,6 +145,28 @@ def test_request_behind_proxy(self):
143145
self.client.request('doesnt matter', 'doesnt matter')
144146
self.assertEqual(proxies, self.session_mock.proxies)
145147

148+
def test_exception_with_details(self):
149+
v1 = MyVersion(self.client)
150+
error_text = """{
151+
"code": 20001,
152+
"message": "Bad request",
153+
"more_info": "https://www.twilio.com/docs/errors/20001",
154+
"status": 400,
155+
"details": {
156+
"foo":"bar"
157+
}
158+
}"""
159+
self.session_mock.send.return_value = Response(400, error_text)
160+
try:
161+
v1.fetch("get", "none", None, None, None, None, None)
162+
self.fail('should not happen')
163+
except TwilioRestException as err:
164+
self.assertEqual(400, err.status)
165+
self.assertEqual(20001, err.code)
166+
self.assertEqual("get", err.method)
167+
self.assertEqual("Unable to fetch record: Bad request", err.msg)
168+
self.assertEqual({"foo": "bar"}, err.details)
169+
146170

147171
class TestHttpClientSession(unittest.TestCase):
148172

@@ -186,3 +210,10 @@ def test_session_not_preserved(self):
186210
# Used different session, responses should be different
187211
self.assertEqual(response_1.content, 'response_1')
188212
self.assertEqual(response_2.content, 'response_2')
213+
214+
215+
class MyVersion(Version):
216+
def __init__(self, domain):
217+
super(MyVersion, self).__init__(domain)
218+
self.version = 'v1'
219+
self._credentials = None

twilio/base/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ class TwilioRestException(TwilioException):
1717
:param str method: The HTTP method used to make the request
1818
:param int|None code: A Twilio-specific error code for the error. This is
1919
not available for all errors.
20+
:param dictionary|None details: Additional error details returned for the exception
2021
"""
2122

22-
def __init__(self, status, uri, msg="", code=None, method='GET'):
23+
def __init__(self, status, uri, msg="", code=None, method='GET', details=None):
2324
self.uri = uri
2425
self.status = status
2526
self.msg = msg
2627
self.code = code
2728
self.method = method
29+
self.details = details
2830

2931
def __str__(self):
3032
""" Try to pretty-print the exception, if this is going on screen. """

twilio/base/version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ def exception(cls, method, uri, response, message):
5757
error_payload = json.loads(response.text)
5858
if 'message' in error_payload:
5959
message = '{}: {}'.format(message, error_payload['message'])
60+
details = error_payload.get('details')
6061
code = error_payload.get('code', response.status_code)
61-
return TwilioRestException(response.status_code, uri, message, code, method)
62+
return TwilioRestException(response.status_code, uri, message, code, method, details)
6263
except Exception:
6364
return TwilioRestException(response.status_code, uri, message, response.status_code, method)
6465

0 commit comments

Comments
 (0)
0