Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Separation of exceptions #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ cover

# PyCharm/IntelliJ
.idea

#htmlcov
*htmlcov*
2 changes: 1 addition & 1 deletion tests/test_available_phonenumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mock import Mock
from nose.tools import assert_equal, assert_true

from twilio import TwilioException
from twilio.rest.exceptions import TwilioException
from twilio.rest.resources import AvailablePhoneNumber
from twilio.rest.resources import AvailablePhoneNumbers
from twilio.rest.resources import PhoneNumbers
Expand Down
2 changes: 1 addition & 1 deletion tests/test_credentials.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from nose.tools import assert_equal, assert_raises
from mock import patch

from twilio import TwilioException
from twilio.rest.exceptions import TwilioException
from twilio.rest import TwilioRestClient, find_credentials


Expand Down
2 changes: 1 addition & 1 deletion tests/test_make_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import twilio
from nose.tools import assert_equal, raises
from mock import patch, Mock, ANY
from twilio import TwilioRestException
from twilio.rest.exceptions import TwilioRestException
from twilio.rest.resources.base import make_request, make_twilio_request
from twilio.rest.resources.connection import Connection
from twilio.rest.resources.connection import PROXY_TYPE_SOCKS5
Expand Down
67 changes: 1 addition & 66 deletions twilio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,7 @@
# -*- coding: utf-8 -*-
import sys

from six import u

# Backwards compatibility.
from .version import __version__, __version_info__


class TwilioException(Exception):
pass


class TwilioRestException(TwilioException):
""" A generic 400 or 500 level exception from the Twilio API

:param int status: the HTTP status that was returned for the exception
:param str uri: The URI that caused the exception
:param str msg: A human-readable message for the error
:param str method: The HTTP method used to make the request
:param int|None code: A Twilio-specific error code for the error. This is
not available for all errors.
"""

# XXX: Move this to the twilio.rest folder

def __init__(self, status, uri, msg="", code=None, method='GET'):
self.uri = uri
self.status = status
self.msg = msg
self.code = code
self.method = method

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

def red(words):
return u("\033[31m\033[49m%s\033[0m") % words

def white(words):
return u("\033[37m\033[49m%s\033[0m") % words

def blue(words):
return u("\033[34m\033[49m%s\033[0m") % words

def teal(words):
return u("\033[36m\033[49m%s\033[0m") % words

def get_uri(code):
return "https://www.twilio.com/docs/errors/{}".format(code)

# If it makes sense to print a human readable error message, try to
# do it. The one problem is that someone might catch this error and
# try to display the message from it to an end user.
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
msg = (
"\n{red_error} {request_was}\n\n{http_line}"
"\n\n{twilio_returned}\n\n{message}\n".format(
red_error=red("HTTP Error"),
request_was=white("Your request was:"),
http_line=teal("%s %s" % (self.method, self.uri)),
twilio_returned=white(
"Twilio returned the following information:"),
message=blue(str(self.msg))
))
if self.code:
msg = "".join([msg, "\n{more_info}\n\n{uri}\n\n".format(
more_info=white("More information may be available here:"),
uri=blue(get_uri(self.code))),
])
return msg
else:
return "HTTP {} error: {}".format(self.status, self.msg)
7 changes: 7 additions & 0 deletions twilio/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
class TwilioException(Exception):
pass


class TwimlException(Exception):
pass
2 changes: 1 addition & 1 deletion twilio/rest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
import platform
from .. import TwilioException
from ..exceptions import TwilioException
from .. import __version__ as LIBRARY_VERSION
from .resources import make_request
from .resources import Accounts
Expand Down
69 changes: 69 additions & 0 deletions twilio/rest/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
import sys

from six import u

# Backwards compatibility.
from ..version import __version__, __version_info__

from ..exceptions import TwilioException


class TwilioRestException(TwilioException):
""" A generic 400 or 500 level exception from the Twilio API

:param int status: the HTTP status that was returned for the exception
:param str uri: The URI that caused the exception
:param str msg: A human-readable message for the error
:param str method: The HTTP method used to make the request
:param int|None code: A Twilio-specific error code for the error. This is
not available for all errors.
"""

def __init__(self, status, uri, msg="", code=None, method='GET'):
self.uri = uri
self.status = status
self.msg = msg
self.code = code
self.method = method

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

def red(words):
return u("\033[31m\033[49m%s\033[0m") % words

def white(words):
return u("\033[37m\033[49m%s\033[0m") % words

def blue(words):
return u("\033[34m\033[49m%s\033[0m") % words

def teal(words):
return u("\033[36m\033[49m%s\033[0m") % words

def get_uri(code):
return "https://www.twilio.com/docs/errors/{}".format(code)

# If it makes sense to print a human readable error message, try to
# do it. The one problem is that someone might catch this error and
# try to display the message from it to an end user.
if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty():
msg = (
"\n{red_error} {request_was}\n\n{http_line}"
"\n\n{twilio_returned}\n\n{message}\n".format(
red_error=red("HTTP Error"),
request_was=white("Your request was:"),
http_line=teal("%s %s" % (self.method, self.uri)),
twilio_returned=white(
"Twilio returned the following information:"),
message=blue(str(self.msg))
))
if self.code:
msg = "".join([msg, "\n{more_info}\n\n{uri}\n\n".format(
more_info=white("More information may be available here:"),
uri=blue(get_uri(self.code))),
])
return msg
else:
return "HTTP {} error: {}".format(self.status, self.msg)
3 changes: 2 additions & 1 deletion twilio/rest/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ... import TwilioException, TwilioRestException
from ...exceptions import TwilioException
from ..exceptions import TwilioRestException

from .imports import (
parse_qs, json, httplib2
Expand Down
4 changes: 3 additions & 1 deletion twilio/rest/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from ...compat import urlparse
from ...compat import urlencode

from ... import __version__, TwilioException, TwilioRestException
from ... import __version__
from ...exceptions import TwilioException
from ..exceptions import TwilioRestException
from .connection import Connection
from .imports import parse_qs, httplib2, json
from .util import transform_params, parse_rfc2822_date, UNSET_TIMEOUT
Expand Down
4 changes: 1 addition & 3 deletions twilio/twiml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"""
import xml.etree.ElementTree as ET


class TwimlException(Exception):
pass
from .exceptions import TwimlException


class Verb(object):
Expand Down