8000 Merge pull request #174 from phalt/develop-paul · DannyTandazo/twilio-python@ff46bfa · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit ff46bfa

Browse files
committed
Merge pull request twilio#174 from phalt/develop-paul
Separation of exceptions
2 parents 29848d1 + c39b0ec commit ff46bfa

File tree

11 files changed

+90
-75
lines changed

11 files changed

+90
-75
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ cover
3030

3131
# PyCharm/IntelliJ
3232
.idea
33+
34+
#htmlcov
35+
*htmlcov*

tests/test_available_phonenumber.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from mock import Mock
44
from nose.tools import assert_equal, assert_true
55

6-
from twilio import TwilioException
6+
from twilio.rest.exceptions import TwilioException
77
from twilio.rest.resources import AvailablePhoneNumber
88
from twilio.rest.resources import AvailablePhoneNumbers
99
from twilio.rest.resources import PhoneNumbers

tests/test_credentials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from nose.tools import assert_equal, assert_raises
22
from mock import patch
33

4-
from twilio import TwilioException
4+
from twilio.rest.exceptions import TwilioException
55
from twilio.rest import TwilioRestClient, find_credentials
66

77

tests/test_make_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import twilio
99
from nose.tools import assert_equal, raises
1010
from mock import patch, Mock, ANY
11-
from twilio import TwilioRestException
11+
from twilio.rest.exceptions import TwilioRestException
1212
from twilio.rest.resources.base import make_request, make_twilio_request
1313
from twilio.rest.resources.connection import Connection
1414
from twilio.rest.resources.connection import PROXY_TYPE_SOCKS5

twilio/__init__.py

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,7 @@
1+
# -*- coding: utf-8 -*-
12
import sys
23

34
from six import u
45

56
# Backwards compatibility.
67
from .version import __version__, __version_info__
7-
8-
9-
class TwilioException(Exception):
10-
pass
11-
12-
13-
class TwilioRestException(TwilioException):
14-
""" A generic 400 or 500 level exception from the Twilio API
15-
16-
:param int status: the HTTP status that was returned for the exception
17-
:param str uri: The URI that caused the exception
18-
:param str msg: A human-readable message for the error
19-
:param str method: The HTTP method used to make the request
20-
:param int|None code: A Twilio-specific error code for the error. This is
21-
not available for all errors.
22-
"""
23-
24-
# XXX: Move this to the twilio.rest folder
25-
26-
def __init__(self, status, uri, msg="", code=None, method='GET'):
27-
self.uri = uri
28-
self.status = status
29-
self.msg = msg
30-
self.code = code
31-
self.method = method
32-
33-
def __str__(self):
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/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
class TwilioException(Exception):
3+
pass
4+
5+
6+
class TwimlException(Exception):
7+
pass

twilio/rest/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import os
33
import platform
4-
from .. import TwilioException
4+
from ..exceptions import TwilioException
55
from .. import __version__ as LIBRARY_VERSION
66
from .resources import make_request
77
from .resources import Accounts

twilio/rest/exceptions.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
import sys
3+
4+
from six import u
5+
6+
# Backwards compatibility.
7+
from ..version import __version__, __version_info__
8+
9+
from ..exceptions import TwilioException
10+
11+
12+
class TwilioRestException(TwilioException):
13+
""" A generic 400 or 500 level exception from the Twilio API
14+
15+
:param int status: the HTTP status that was returned for the exception
16+
:param str uri: The URI that caused the exception
17+
:param str msg: A human-readable message for the error
18+
:param str method: The HTTP method used to make the request
19+
:param int|None code: A Twilio-specific error code for the error. This is
20+
not available for all errors.
21+
"""
22+
23+
def __init__(self, status, uri, msg="", code=None, method='GET'):
24+
self.uri = uri
25+
self.status = status
26+
self.msg = msg
27+
self.code = code
28+
self.method = method
29+
30+
def __str__(self):
31+
""" Try to pretty-print the exception, if this is going on screen. """
32+
33+
def red(words):
34+
return u("\033[31m\033[49m%s\033[0m") % words
35+
36+
def white(words):
37+
return u("\033[37m\033[49m%s\033[0m") % words
38+
39+
def blue(words):
40+
return u("\033[34m\033[49m%s\033[0m") % words
41+
42+
def teal(words):
43+
return u("\033[36m\033[49m%s\033[0m") % words
44+
45+
def get_uri(code):
46+
return "https://www.twilio.com/docs/errors/{}".format(code)
47+
48+
# If it makes sense to print a human readable error message, try to
49+
# do it. The one problem is that someone might catch this error and
50+
# try to display the message from it to an end user.
51+
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
52+
msg = (
53+
"\n{red_error} {request_was}\n\n{http_line}"
54+
"\n\n{twilio_returned}\n\n{message}\n".format(
55+
red_error=red("HTTP Error"),
56+
request_was=white("Your request was:"),
57+
http_line=teal("%s %s" % (self.method, self.uri)),
58+
twilio_returned=white(
59+
"Twilio returned the following information:"),
60+
message=blue(str(self.msg))
61+
))
62+
if self.code:
63+
msg = "".join([msg, "\n{more_info}\n\n{uri}\n\n".format(
64+
more_info=white("More information may be available here:"),
65+
uri=blue(get_uri(self.code))),
66+
])
67+
return msg
68+
else:
69+
return "HTTP {} error: {}".format(self.status, self.msg)

twilio/rest/resources/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from ... import TwilioException, TwilioRestException
1+
from ...exceptions import TwilioException
2+
from ..exceptions import TwilioRestException
23

34
from .imports import (
45
parse_qs, json, httplib2

twilio/rest/resources/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from ...compat import urlparse
77
from ...compat import urlencode
88

9-
from ... import __version__, TwilioException, TwilioRestException
9+
from ... import __version__
10+
from ...exceptions import TwilioException
11+
from ..exceptions import TwilioRestException
1012
from .connection import Connection
1113
from .imports import parse_qs, httplib2, json
1214
from .util import transform_params, parse_rfc2822_date, UNSET_TIMEOUT

twilio/twiml.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"""
44
import xml.etree.ElementTree as ET
55

6-
7-
class TwimlException(Exception):
8-
pass
6+
from .exceptions import TwimlException
97

108

119
class Verb(object):

0 commit comments

Comments
 (0)
0