Skip to content

Commit

Permalink
Merge pull request #611 from obskyr/master
Browse files Browse the repository at this point in the history
Added RateLimitError for easily working with the rate limit.
  • Loading branch information
joshthecoder committed May 29, 2015
2 parents 0279a4e + c2e32f8 commit 5f06cf9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ Jeff Hull (@jsh2134)
Mike (mikeandmore)
Kohei YOSHIDA
Mark Smith (@judy2k)
Steven Skoczen (@skoczen)
Steven Skoczen (@skoczen)
Samuel (@obskyr)
2 changes: 1 addition & 1 deletion tweepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
__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
Expand Down
8 changes: 6 additions & 2 deletions tweepy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

Expand Down Expand Up @@ -220,7 +220,11 @@ def execute(self):
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)
Expand Down
14 changes: 13 additions & 1 deletion tweepy/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import six


class TweepError(Exception):
"""Tweepy exception"""

Expand All @@ -17,3 +16,16 @@ def __init__(self, reason, response=None):

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

0 comments on commit 5f06cf9

Please sign in to comment.