subclass api errors
This commit is contained in:
pare
1d54c35101
commit
e97dec7306
S'han modificat 1 arxius amb 45 adicions i 29 eliminacions
|
@ -1550,20 +1550,14 @@ 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']
|
||||||
if isinstance(response, dict) and 'error' in response:
|
except ValueError:
|
||||||
raise MastodonAPIError("Mastodon API returned error: " + str(response['error']))
|
error_msg = None
|
||||||
else:
|
|
||||||
raise MastodonAPIError('Endpoint not found.')
|
|
||||||
|
|
||||||
|
|
||||||
if response_object.status_code == 500:
|
|
||||||
raise MastodonAPIError('General API problem.')
|
|
||||||
|
|
||||||
# Handle rate limiting
|
# Handle rate limiting
|
||||||
if response_object.status_code == 429:
|
if response_object.status_code == 429:
|
||||||
|
@ -1578,6 +1572,24 @@ class Mastodon:
|
||||||
request_complete = False
|
request_complete = False
|
||||||
continue
|
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:
|
||||||
|
ex_type = MastodonAPIError
|
||||||
|
|
||||||
|
raise ex_type(
|
||||||
|
'Mastodon API returned error',
|
||||||
|
response_object.status_code,
|
||||||
|
response_object.reason,
|
||||||
|
error_msg)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = response_object.json(object_hook=self.__json_hooks)
|
response = response_object.json(object_hook=self.__json_hooks)
|
||||||
except:
|
except:
|
||||||
|
@ -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