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 text content: ' + str(response_object.text))
|
||||
|
||||
if response_object.status_code == 404:
|
||||
if not response_object.ok:
|
||||
try:
|
||||
response = response_object.json()
|
||||
except:
|
||||
raise MastodonAPIError('Endpoint not found.')
|
||||
|
||||
if isinstance(response, dict) and 'error' in response:
|
||||
raise MastodonAPIError("Mastodon API returned error: " + str(response['error']))
|
||||
else:
|
||||
raise MastodonAPIError('Endpoint not found.')
|
||||
|
||||
|
||||
if response_object.status_code == 500:
|
||||
raise MastodonAPIError('General API problem.')
|
||||
response = response_object.json(object_hook=self.__json_hooks)
|
||||
if not isinstance(response, dict) or 'error' not in response:
|
||||
error_msg = None
|
||||
error_msg = response['error']
|
||||
except ValueError:
|
||||
error_msg = None
|
||||
|
||||
# Handle rate limiting
|
||||
if response_object.status_code == 429:
|
||||
|
@ -1578,6 +1572,24 @@ class Mastodon:
|
|||
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:
|
||||
ex_type = MastodonAPIError
|
||||
|
||||
raise ex_type(
|
||||
'Mastodon API returned error',
|
||||
response_object.status_code,
|
||||
response_object.reason,
|
||||
error_msg)
|
||||
|
||||
try:
|
||||
response = response_object.json(object_hook=self.__json_hooks)
|
||||
except:
|
||||
|
@ -1586,12 +1598,6 @@ class Mastodon:
|
|||
"bad json content was '%s'" % (response_object.status_code,
|
||||
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
|
||||
if isinstance(response, list) 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"""
|
||||
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):
|
||||
"""Raised when rate limiting is set to manual mode and the rate limit is exceeded"""
|
||||
|
|
Loading…
Referencia en una nova incidència