8000 Merge branch 'restexception-changes' · PNPtutorials/twilio-python@9d8dc20 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d8dc20

Browse files
author
Kevin Burke
committed
Merge branch 'restexception-changes'
2 parents 58fb6bc + 650c4dd commit 9d8dc20

File tree

4 files changed

+57
-39
lines changed

4 files changed

+57
-39
lines changed

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import sys
2-
from twilio import __version__
32
from setuptools import setup, find_packages
43

4+
__version__ = None
5+
with open('twilio/version.py') as f:
6+
exec(f.read())
7+
58
# To install the twilio-python library, open a Terminal shell, then run this
69
# file by typing:
710
#

twilio/__init__.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
__version_info__ = ('3', '6', '5')
2-
__version__ = '.'.join(__version_info__)
1+
import sys
2+
3+
from six import u
4+
5+
# Backwards compatibility.
6+
from .version import __version__, __version_info__
37

48

59
class TwilioException(Exception):
@@ -12,17 +16,57 @@ class TwilioRestException(TwilioException):
1216
:param int status: the HTTP status that was returned for the exception
1317
:param str uri: The URI that caused the exception
1418
:param str msg: A human-readable message for the error
19+
:param str method: The HTTP method used to make the request
1520
:param int|None code: A Twilio-specific error code for the error. This is
1621
not available for all errors.
1722
"""
1823

1924
# XXX: Move this to the twilio.rest folder
2025

21-
def __init__(self, status, uri, msg="", code=None):
26+
def __init__(self, status, uri, msg="", code=None, method='GET'):
2227
self.uri = uri
2328
self.status = status
2429
self.msg = msg
2530
self.code = code
31+
self.method = method
2632

2733
def __str__(self):
28-
return "HTTP ERROR %s: %s \n %s" % (self.status, self.msg, self.uri)
34+
""" Try to pretty-print the exception, if this is going on screen. """
35+
36+
def red(words):
37+
return u("\033[31m\033[49m%s\033[0m") % words
38+
39+
def white(words):
40+
return u("\033[37m\033[49m%s\033[0m") % words
41+
42+
def blue(words):
43+
return u("\033[34m\033[49m%s\033[0m") % words
44+
45+
def teal(words):
46+
return u("\033[36m\033[49m%s\033[0m") % words
47+
48+
def get_uri(code):
49+
return "https://www.twilio.com/docs/errors/{}".format(code)
50+
51+
# If it makes sense to print a human readable error message, try to
52+
# do it. The one problem is that someone might catch this error and
53+
# try to display the message from it to an end user.
54+
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
55+
msg = (
56+
"\n{red_error} {request_was}\n\n{http_line}"
57+
"\n\n{twilio_returned}\n\n{message}\n".format(
58+
red_error=red("HTTP Error"),
59+
request_was=white("Your request was:"),
60+
http_line=teal("%s %s" % (self.method, self.uri)),
61+
twilio_returned=white(
62+
"Twilio returned the following information:"),
63+
message=blue(str(self.msg))
64+
))
65+
if self.code:
66+
msg = "".join([msg, "\n{more_info}\n\n{uri}\n\n".format(
67+
more_info=white("More information may be available here:"),
68+
uri=blue(get_uri(self.code))),
69+
])
70+
return msg
71+
else:
72+
return "HTTP {} error: {}".format(self.status, self.msg)

twilio/rest/resources/base.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import logging
22
import os
33
import platform
4-
import sys
54

6-
from six import integer_types, string_types, binary_type, iteritems, u
5+
from six import integer_types, string_types, binary_type, iteritems
76
from ...compat import urlparse
87
from ...compat import urlencode
98

@@ -144,38 +143,8 @@ def make_twilio_request(method, uri, **kwargs):
144143
code = None
145144
message = resp.content
146145

147-
def red(msg):
148-
return u("\033[31m\033[49m%s\033[0m") % msg
149-
150-
def white(msg):
151-
return u("\033[37m\033[49m%s\033[0m") % msg
152-
153-
def blue(msg):
154-
return u("\033[34m\033[49m%s\033[0m") % msg
155-
156-
def orange(msg):
157-
return u("\033[33m\033[49m%s\033[0m") % msg
158-
159-
def teal(msg):
160-
return u("\033[36m\033[49m%s\033[0m") % msg
161-
162-
# If it makes sense to print a human readable error message, try to do
163-
# it. The one problem is that someone might catch this error and try to
164-
# display the message from it to an end user.
165-
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
166-
msg = red("\nHTTP Error. ")
167-
msg += white("Your request was:\n\n")
168-
msg += teal("%s %s" % (method, uri))
169-
msg += white("\n\nTwilio returned the following information:")
170-
msg += blue("\n\n" + str(message) + "\n")
171-
if code:
172-
msg += white("\nMore information may be available here:\n\n")
173-
msg += blue("https://www.twilio.com/docs/errors/%s" % code)
174-
msg += "\n\n"
175-
else:
176-
msg = message
177-
178-
raise TwilioRestException(resp.status_code, resp.url, msg, code)
146+
raise TwilioRestException(status=resp.status_code, method=method,
147+
uri=resp.url, msg=message, code=code)
179148

180149
return resp
181150

twilio/version.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__version_info__ = ('3', '6', '5')
2+
__version__ = '.'.join(__version_info__)

0 commit comments

Comments
 (0)
0