Merge pull request #97 from terrycojones/call-parent-init-in-exception-classes-96
call-parent-init-in-exception-classes-96
This commit is contained in:
commit
79842875be
1 changed files with 16 additions and 26 deletions
|
|
@ -9,7 +9,7 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__author__ = "Ryan McGrath <ryan@venodesigns.net>"
|
__author__ = "Ryan McGrath <ryan@venodesigns.net>"
|
||||||
__version__ = "2.0.1"
|
__version__ = "2.0.2"
|
||||||
|
|
||||||
import urllib
|
import urllib
|
||||||
import re
|
import re
|
||||||
|
|
@ -68,11 +68,6 @@ class TwythonError(Exception):
|
||||||
twitter_http_status_codes[error_code][1],
|
twitter_http_status_codes[error_code][1],
|
||||||
self.msg)
|
self.msg)
|
||||||
|
|
||||||
if error_code == 420:
|
|
||||||
raise TwythonRateLimitError(self.msg,
|
|
||||||
error_code,
|
|
||||||
retry_after=retry_after)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return repr(self.msg)
|
return repr(self.msg)
|
||||||
|
|
||||||
|
|
@ -81,12 +76,7 @@ class TwythonAuthError(TwythonError):
|
||||||
""" Raised when you try to access a protected resource and it fails due to
|
""" Raised when you try to access a protected resource and it fails due to
|
||||||
some issue with your authentication.
|
some issue with your authentication.
|
||||||
"""
|
"""
|
||||||
def __init__(self, msg, error_code=None):
|
pass
|
||||||
self.msg = msg
|
|
||||||
self.error_code = error_code
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return repr(self.msg)
|
|
||||||
|
|
||||||
|
|
||||||
class TwythonRateLimitError(TwythonError):
|
class TwythonRateLimitError(TwythonError):
|
||||||
|
|
@ -94,12 +84,9 @@ class TwythonRateLimitError(TwythonError):
|
||||||
retry_wait_seconds is the number of seconds to wait before trying again.
|
retry_wait_seconds is the number of seconds to wait before trying again.
|
||||||
"""
|
"""
|
||||||
def __init__(self, msg, error_code, retry_after=None):
|
def __init__(self, msg, error_code, retry_after=None):
|
||||||
|
TwythonError.__init__(self, msg, error_code=error_code)
|
||||||
if isinstance(retry_after, int):
|
if isinstance(retry_after, int):
|
||||||
retry_after = int(retry_after)
|
self.msg = '%s (Retry after %d seconds)' % (msg, retry_after)
|
||||||
self.msg = '%s (Retry after %s seconds)' % (msg, retry_after)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return repr(self.msg)
|
|
||||||
|
|
||||||
|
|
||||||
class Twython(object):
|
class Twython(object):
|
||||||
|
|
@ -229,16 +216,19 @@ class Twython(object):
|
||||||
raise TwythonError('Response was not valid JSON, unable to decode.')
|
raise TwythonError('Response was not valid JSON, unable to decode.')
|
||||||
|
|
||||||
if response.status_code > 304:
|
if response.status_code > 304:
|
||||||
# Just incase there is no error message, let's set a default
|
# If there is no error message, use a default.
|
||||||
error_msg = 'An error occurred processing your request.'
|
error_msg = content.get(
|
||||||
if content.get('error') is not None:
|
'error', 'An error occurred processing your request.')
|
||||||
error_msg = content['error']
|
|
||||||
|
|
||||||
self._last_call['api_error'] = error_msg
|
self._last_call['api_error'] = error_msg
|
||||||
|
|
||||||
raise TwythonError(error_msg,
|
if response.status_code == 420:
|
||||||
error_code=response.status_code,
|
exceptionType = TwythonRateLimitError
|
||||||
retry_after=response.headers.get('retry-after'))
|
else:
|
||||||
|
exceptionType = TwythonError
|
||||||
|
|
||||||
|
raise exceptionType(error_msg,
|
||||||
|
error_code=response.status_code,
|
||||||
|
retry_after=response.headers.get('retry-after'))
|
||||||
|
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
@ -363,7 +353,7 @@ class Twython(object):
|
||||||
if request.status_code in [301, 201, 200]:
|
if request.status_code in [301, 201, 200]:
|
||||||
return request.text
|
return request.text
|
||||||
else:
|
else:
|
||||||
raise TwythonError('shortenURL() failed with a %s error code.' % request.status_code, request.status_code)
|
raise TwythonError('shortenURL() failed with a %s error code.' % request.status_code)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def constructApiURL(base_url, params):
|
def constructApiURL(base_url, params):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue