From 3e752d71a9a0f11d425282deba1698133411681d Mon Sep 17 00:00:00 2001 From: obskyr Date: Mon, 18 May 2015 20:22:50 +0200 Subject: [PATCH] Added RateLimitError. Resolves #600. Can't believe this hasn't been in until now! Huh. --- tweepy/__init__.py | 2 +- tweepy/binder.py | 8 ++++++-- tweepy/error.py | 14 +++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/tweepy/__init__.py b/tweepy/__init__.py index b691e34..1125d1c 100644 --- a/tweepy/__init__.py +++ b/tweepy/__init__.py @@ -10,7 +10,7 @@ __author__ = 'Joshua Roesslein' __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 diff --git a/tweepy/binder.py b/tweepy/binder.py index 2ac6146..ba55570 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -12,7 +12,7 @@ import requests 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 @@ -220,7 +220,11 @@ def bind_api(**config): 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) diff --git a/tweepy/error.py b/tweepy/error.py index 1c47a5a..7827029 100644 --- a/tweepy/error.py +++ b/tweepy/error.py @@ -6,7 +6,6 @@ from __future__ import print_function import six - class TweepError(Exception): """Tweepy exception""" @@ -17,3 +16,16 @@ class TweepError(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 -- 2.25.1