Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a RateLimitError for handling... well, rate limit errors. #600

Closed
obskyr opened this issue Apr 23, 2015 · 2 comments
Closed

Add a RateLimitError for handling... well, rate limit errors. #600

obskyr opened this issue Apr 23, 2015 · 2 comments

Comments

@obskyr
Copy link
Contributor

obskyr commented Apr 23, 2015

As it is right now, seemingly all errors you might bump into using Tweepy are of the class tweepy.TweepError, which works fine in many cases - but not in all of them. Very often it's really useful to identify a rate limit error specifically, as a certain type of handling is required for it.

So far, I've been catching all TweepErrors, checking if they're of the rate limit variety, handling them if they are and letting them through if they're not. This is cumbersome and could be made much easier for everyone by adding a RateLimitError, and the error handling would both be easy and readable.

The code I've been using so far is along the lines of:

def is_rate_limit_error(e):
    return isinstance(e.message, list) \
        and e.message[0:] \
        and 'code' in e.message[0] \
        and e.message[0]['code'] == 88

Feel free to use that very function (or something else). Adding a RateLimitError would transform this (pseudo-)code:

# [function definition for is_rate_limit_error, cut out for your reading pleasure]
try:
    api.do_twitter_things()
except tweepy.TweepError as e:
    if not is_rate_limit_error(e):
        raise e
    handle_rate_limit_error()

Into this:

try:
    api.do_twitter_things()
except tweepy.RateLimitError:
    handle_rate_limit_error()

Adding this to Tweepy would not break any programs written earlier - let RateLimitError subclass TweepError, and all earlier code should work just fine.

@geransmith
Copy link
Contributor

I am definitely for this kind of code main lined. I am going to look into adding this to my local copy, as my janky workaround is not nearly as pretty as this. If you can provide more info or attach this to a pull request that would be wicked helpful.

@obskyr
Copy link
Contributor Author

obskyr commented May 18, 2015

If anyone's wondering, I suppose I'll write a little bit about how this fix / identification works (@tehspaceg, here's the "more info" you mentioned).

Basically, TweepErrors can be raised at different times by Tweepy. Most of the time, they're raised with a string as the message (or "reason"). However, in the place where TweepErrors are raised because of errors Twitter returned, the message is set to a piece of parsed and loaded JSON that Twitter returns. This JSON might look one of a number of different ways depending on the error.

This means that, in order for an error to be a RateLimitError, its message needs to:

  • Not be a plain string, since those are never returned by Twitter itself
  • Be a list, since that's the format Twitter returns the error in
  • Have an entry - the error, in object (or, in Python's case, dict) form

This entry then needs to:

  • Have an error code...
  • ...which for rate limit errors is 88.

So this is what each line in is_rate_limit_error deals with.

I've submitted a pull request; let's hope there's nothing in the way!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants