diff --git a/twython/__init__.py b/twython/__init__.py index 26a3860..97be55a 100644 --- a/twython/__init__.py +++ b/twython/__init__.py @@ -1,2 +1,2 @@ from twython import Twython -from twython import TwythonError, TwythonRateLimitError, TwythonAuthError +from .exceptions import TwythonError, TwythonRateLimitError, TwythonAuthError diff --git a/twython/exceptions.py b/twython/exceptions.py new file mode 100644 index 0000000..7c939af --- /dev/null +++ b/twython/exceptions.py @@ -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) diff --git a/twython/twython.py b/twython/twython.py index bf66740..415b145 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -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'):