aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/Mastodon.py78
1 files changed, 47 insertions, 31 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 28ad9d8..5aca185 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -1550,33 +1550,45 @@ class Mastodon:
1550 print('response headers: ' + str(response_object.headers)) 1550 print('response headers: ' + str(response_object.headers))
1551 print('Response text content: ' + str(response_object.text)) 1551 print('Response text content: ' + str(response_object.text))
1552 1552
1553 if response_object.status_code == 404: 1553 if not response_object.ok:
1554 try: 1554 try:
1555 response = response_object.json() 1555 response = response_object.json(object_hook=self.__json_hooks)
1556 except: 1556 if not isinstance(response, dict) or 'error' not in response:
1557 raise MastodonAPIError('Endpoint not found.') 1557 error_msg = None
1558 1558 error_msg = response['error']
1559 if isinstance(response, dict) and 'error' in response: 1559 except ValueError:
1560 raise MastodonAPIError("Mastodon API returned error: " + str(response['error'])) 1560 error_msg = None
1561
1562 # Handle rate limiting
1563 if response_object.status_code == 429:
1564 if self.ratelimit_method == 'throw' or not do_ratelimiting:
1565 raise MastodonRatelimitError('Hit rate limit.')
1566 elif self.ratelimit_method in ('wait', 'pace'):
1567 to_next = self.ratelimit_reset - time.time()
1568 if to_next > 0:
1569 # As a precaution, never sleep longer than 5 minutes
1570 to_next = min(to_next, 5 * 60)
1571 time.sleep(to_next)
1572 request_complete = False
1573 continue
1574
1575 if response_object.status_code == 404:
1576 ex_type = MastodonNotFoundError
1577 if not error_msg:
1578 error_msg = 'Endpoint not found.'
1579 # this is for compatibility with older versions
1580 # which raised MastodonAPIError('Endpoint not found.')
1581 # on any 404
1582 elif response_object.status_code == 401:
1583 ex_type = MastodonUnauthorizedError
1561 else: 1584 else:
1562 raise MastodonAPIError('Endpoint not found.') 1585 ex_type = MastodonAPIError
1563 1586
1564 1587 raise ex_type(
1565 if response_object.status_code == 500: 1588 'Mastodon API returned error',
1566 raise MastodonAPIError('General API problem.') 1589 response_object.status_code,
1567 1590 response_object.reason,
1568 # Handle rate limiting 1591 error_msg)
1569 if response_object.status_code == 429:
1570 if self.ratelimit_method == 'throw' or not do_ratelimiting:
1571 raise MastodonRatelimitError('Hit rate limit.')
1572 elif self.ratelimit_method in ('wait', 'pace'):
1573 to_next = self.ratelimit_reset - time.time()
1574 if to_next > 0:
1575 # As a precaution, never sleep longer than 5 minutes
1576 to_next = min(to_next, 5 * 60)
1577 time.sleep(to_next)
1578 request_complete = False
1579 continue
1580 1592
1581 try: 1593 try:
1582 response = response_object.json(object_hook=self.__json_hooks) 1594 response = response_object.json(object_hook=self.__json_hooks)
@@ -1586,12 +1598,6 @@ class Mastodon:
1586 "bad json content was '%s'" % (response_object.status_code, 1598 "bad json content was '%s'" % (response_object.status_code,
1587 response_object.content)) 1599 response_object.content))
1588 1600
1589 # See if the returned dict is an error dict even though status is 200
1590 if isinstance(response, dict) and 'error' in response:
1591 if not isinstance(response['error'], six.string_types):
1592 response['error'] = six.text_type(response['error'])
1593 raise MastodonAPIError("Mastodon API returned error: " + response['error'])
1594
1595 # Parse link headers 1601 # Parse link headers
1596 if isinstance(response, list) and \ 1602 if isinstance(response, list) and \
1597 'Link' in response_object.headers and \ 1603 'Link' in response_object.headers and \
@@ -1801,6 +1807,16 @@ class MastodonAPIError(MastodonError):
1801 """Raised when the mastodon API generates a response that cannot be handled""" 1807 """Raised when the mastodon API generates a response that cannot be handled"""
1802 pass 1808 pass
1803 1809
1810class MastodonNotFoundError(MastodonAPIError):
1811 """Raised when the mastodon API returns a 404 Not Found error"""
1812 pass
1813
1814class MastodonUnauthorizedError(MastodonAPIError):
1815 """Raised when the mastodon API returns a 401 Unauthorized error
1816
1817 This happens when an OAuth token is invalid or has been revoked."""
1818 pass
1819
1804 1820
1805class MastodonRatelimitError(MastodonError): 1821class MastodonRatelimitError(MastodonError):
1806 """Raised when rate limiting is set to manual mode and the rate limit is exceeded""" 1822 """Raised when rate limiting is set to manual mode and the rate limit is exceeded"""
Powered by cgit v1.2.3 (git 2.41.0)