subclass api errors
This commit is contained in:
pare
1d54c35101
commit
e97dec7306
S'han modificat 1 arxius amb 45 adicions i 29 eliminacions
|
@ -1550,33 +1550,45 @@ class Mastodon:
|
||||||
print('response headers: ' + str(response_object.headers))
|
print('response headers: ' + str(response_object.headers))
|
||||||
print('Response text content: ' + str(response_object.text))
|
print('Response text content: ' + str(response_object.text))
|
||||||
|
|
||||||
if response_object.status_code == 404:
|
if not response_object.ok:
|
||||||
try:
|
try:
|
||||||
response = response_object.json()
|
response = response_object.json(object_hook=self.__json_hooks)
|
||||||
except:
|
if not isinstance(response, dict) or 'error' not in response:
|
||||||
raise MastodonAPIError('Endpoint not found.')
|
error_msg = None
|
||||||
|
error_msg = response['error']
|
||||||
|
except ValueError:
|
||||||
|
error_msg = None
|
||||||
|
|
||||||
if isinstance(response, dict) and 'error' in response:
|
# Handle rate limiting
|
||||||
raise MastodonAPIError("Mastodon API returned error: " + str(response['error']))
|
if response_object.status_code == 429:
|
||||||
|
if self.ratelimit_method == 'throw' or not do_ratelimiting:
|
||||||
|
raise MastodonRatelimitError('Hit rate limit.')
|
||||||
|
elif self.ratelimit_method in ('wait', 'pace'):
|
||||||
|
to_next = self.ratelimit_reset - time.time()
|
||||||
|
if to_next > 0:
|
||||||
|
# As a precaution, never sleep longer than 5 minutes
|
||||||
|
to_next = min(to_next, 5 * 60)
|
||||||
|
time.sleep(to_next)
|
||||||
|
request_complete = False
|
||||||
|
continue
|
||||||
|
|
||||||
|
if response_object.status_code == 404:
|
||||||
|
ex_type = MastodonNotFoundError
|
||||||
|
if not error_msg:
|
||||||
|
error_msg = 'Endpoint not found.'
|
||||||
|
# this is for compatibility with older versions
|
||||||
|
# which raised MastodonAPIError('Endpoint not found.')
|
||||||
|
# on any 404
|
||||||
|
elif response_object.status_code == 401:
|
||||||
|
ex_type = MastodonUnauthorizedError
|
||||||
else:
|
else:
|
||||||
raise MastodonAPIError('Endpoint not found.')
|
ex_type = MastodonAPIError
|
||||||
|
|
||||||
|
raise ex_type(
|
||||||
if response_object.status_code == 500:
|
'Mastodon API returned error',
|
||||||
raise MastodonAPIError('General API problem.')
|
response_object.status_code,
|
||||||
|
response_object.reason,
|
||||||
# Handle rate limiting
|
error_msg)
|
||||||
if response_object.status_code == 429:
|
|
||||||
if self.ratelimit_method == 'throw' or not do_ratelimiting:
|
|
||||||
raise MastodonRatelimitError('Hit rate limit.')
|
|
||||||
elif self.ratelimit_method in ('wait', 'pace'):
|
|
||||||
to_next = self.ratelimit_reset - time.time()
|
|
||||||
if to_next > 0:
|
|
||||||
# As a precaution, never sleep longer than 5 minutes
|
|
||||||
to_next = min(to_next, 5 * 60)
|
|
||||||
time.sleep(to_next)
|
|
||||||
request_complete = False
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = response_object.json(object_hook=self.__json_hooks)
|
response = response_object.json(object_hook=self.__json_hooks)
|
||||||
|
@ -1586,12 +1598,6 @@ class Mastodon:
|
||||||
"bad json content was '%s'" % (response_object.status_code,
|
"bad json content was '%s'" % (response_object.status_code,
|
||||||
response_object.content))
|
response_object.content))
|
||||||
|
|
||||||
# See if the returned dict is an error dict even though status is 200
|
|
||||||
if isinstance(response, dict) and 'error' in response:
|
|
||||||
if not isinstance(response['error'], six.string_types):
|
|
||||||
response['error'] = six.text_type(response['error'])
|
|
||||||
raise MastodonAPIError("Mastodon API returned error: " + response['error'])
|
|
||||||
|
|
||||||
# Parse link headers
|
# Parse link headers
|
||||||
if isinstance(response, list) and \
|
if isinstance(response, list) and \
|
||||||
'Link' in response_object.headers and \
|
'Link' in response_object.headers and \
|
||||||
|
@ -1801,6 +1807,16 @@ class MastodonAPIError(MastodonError):
|
||||||
"""Raised when the mastodon API generates a response that cannot be handled"""
|
"""Raised when the mastodon API generates a response that cannot be handled"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class MastodonNotFoundError(MastodonAPIError):
|
||||||
|
"""Raised when the mastodon API returns a 404 Not Found error"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MastodonUnauthorizedError(MastodonAPIError):
|
||||||
|
"""Raised when the mastodon API returns a 401 Unauthorized error
|
||||||
|
|
||||||
|
This happens when an OAuth token is invalid or has been revoked."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class MastodonRatelimitError(MastodonError):
|
class MastodonRatelimitError(MastodonError):
|
||||||
"""Raised when rate limiting is set to manual mode and the rate limit is exceeded"""
|
"""Raised when rate limiting is set to manual mode and the rate limit is exceeded"""
|
||||||
|
|
Loading…
Referencia en una nova incidència