Can't believe this hasn't been in until now! Huh.
__license__ = 'MIT'
from tweepy.models import Status, User, DirectMessage, Friendship, SavedSearch, SearchResults, ModelFactory, Category
-from tweepy.error import TweepError
+from tweepy.error import TweepError, RateLimitError
from tweepy.api import API
from tweepy.cache import Cache, MemoryCache, FileCache
from tweepy.auth import OAuthHandler, AppAuthHandler
import logging
-from tweepy.error import TweepError
+from tweepy.error import TweepError, RateLimitError, is_rate_limit_error_message
from tweepy.utils import convert_to_utf8_str
from tweepy.models import Model
error_msg = self.parser.parse_error(resp.text)
except Exception:
error_msg = "Twitter error response: status code = %s" % resp.status_code
- raise TweepError(error_msg, resp)
+
+ if is_rate_limit_error_message(error_msg):
+ raise RateLimitError(error_msg, resp)
+ else:
+ raise TweepError(error_msg, resp)
# Parse the response payload
result = self.parser.parse(self, resp.text)
import six
-
class TweepError(Exception):
"""Tweepy exception"""
def __str__(self):
return self.reason
+
+def is_rate_limit_error_message(message):
+ """Check if the supplied error message belongs to a rate limit error."""
+ return isinstance(message, list) \
+ and len(message) > 0 \
+ and 'code' in message[0] \
+ and message[0]['code'] == 88
+
+class RateLimitError(TweepError):
+ """Exception for Tweepy hitting the rate limit."""
+ # RateLimitError has the exact same properties and inner workings
+ # as TweepError for backwards compatibility reasons.
+ pass