Merge pull request #181 from ryanmcgrath/extract-exceptions

Move Exceptions to own file
This commit is contained in:
Mike Helmick 2013-04-15 13:13:28 -07:00
commit 561e4104fc
3 changed files with 51 additions and 44 deletions

View file

@ -1,2 +1,2 @@
from twython import Twython
from twython import TwythonError, TwythonRateLimitError, TwythonAuthError
from .exceptions import TwythonError, TwythonRateLimitError, TwythonAuthError

48
twython/exceptions.py Normal file
View file

@ -0,0 +1,48 @@
from twitter_endpoints import twitter_http_status_codes
class TwythonError(Exception):
"""
Generic error class, catch-all for most Twython issues.
Special cases are handled by TwythonAuthError & TwythonRateLimitError.
Note: Syntax has changed as of Twython 1.3. To catch these,
you need to explicitly import them into your code, e.g:
from twython import (
TwythonError, TwythonRateLimitError, TwythonAuthError
)
"""
def __init__(self, msg, error_code=None, retry_after=None):
self.error_code = error_code
if error_code is not None and error_code in twitter_http_status_codes:
msg = 'Twitter API returned a %s (%s), %s' % \
(error_code,
twitter_http_status_codes[error_code][0],
msg)
super(TwythonError, self).__init__(msg)
@property
def msg(self):
return self.args[0]
class TwythonAuthError(TwythonError):
""" Raised when you try to access a protected resource and it fails due to
some issue with your authentication.
"""
pass
class TwythonRateLimitError(TwythonError):
""" Raised when you've hit a rate limit.
The amount of seconds to retry your request in will be appended
to the message.
"""
def __init__(self, msg, error_code, retry_after=None):
if isinstance(retry_after, int):
msg = '%s (Retry after %d seconds)' % (msg, retry_after)
TwythonError.__init__(self, msg, error_code=error_code)

View file

@ -23,7 +23,8 @@ except ImportError:
# Twython maps keyword based arguments to Twitter API endpoints. The endpoints
# table is a file with a dictionary of every API endpoint that Twython supports.
from twitter_endpoints import base_url, api_table, twitter_http_status_codes
from twitter_endpoints import base_url, api_table
from .exceptions import TwythonError, TwythonAuthError, TwythonRateLimitError
try:
import simplejson as json
@ -31,48 +32,6 @@ except ImportError:
import json
class TwythonError(Exception):
"""
Generic error class, catch-all for most Twython issues.
Special cases are handled by TwythonAPILimit and TwythonAuthError.
Note: To use these, the syntax has changed as of Twython 1.3. To catch these,
you need to explicitly import them into your code, e.g:
from twython import TwythonError, TwythonRateLimitError, TwythonAuthError
"""
def __init__(self, msg, error_code=None, retry_after=None):
self.error_code = error_code
if error_code is not None and error_code in twitter_http_status_codes:
msg = 'Twitter API returned a %s (%s), %s' % (error_code,
twitter_http_status_codes[error_code][0],
msg)
super(TwythonError, self).__init__(msg)
@property
def msg(self):
return self.args[0]
class TwythonAuthError(TwythonError):
""" Raised when you try to access a protected resource and it fails due to
some issue with your authentication.
"""
pass
class TwythonRateLimitError(TwythonError):
""" Raised when you've hit a rate limit.
retry_wait_seconds is the number of seconds to wait before trying again.
"""
def __init__(self, msg, error_code, retry_after=None):
if isinstance(retry_after, int):
msg = '%s (Retry after %d seconds)' % (msg, retry_after)
TwythonError.__init__(self, msg, error_code=error_code)
class Twython(object):
def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None,
headers=None, callback_url=None, twitter_token=None, twitter_secret=None, proxies=None, version='1.1'):