sferik / twitter-ruby

A Ruby interface to the Twitter API.
http://www.rubydoc.info/gems/twitter
MIT License
4.58k stars 1.31k forks source link

JSON Parser Error with Twitter Stream rate limit #930

Closed fffelix closed 1 year ago

fffelix commented 6 years ago

Hi,

I am using the gem to work with the twitter stream, but I get

[..]/.rbenv/versions/2.5.0/lib/ruby/2.5.0/json/common.rb:156:in `parse': 765: unexpected token at 'Exceeded connection limit for user' (JSON::ParserError)

errors (other variant of message is "unexpected token at 'Easy there, Turbo. Too many requests recently. Enhance your calm.' (JSON::ParserError)") whenever I exceed the rate limit.

I'm using the exception handling from the examples to catch the too many requests error like this:

begin
    stream_client.filter(track: topics.join(","), tweet_mode: "extended") do |object|
        if object.is_a?(Twitter::Tweet)
            ap object
        end
    end
rescue Twitter::Error::TooManyRequests => error
   puts "Rate limited on initial fetch, sleeping for #{error.rate_limit.reset_in + 1} seconds"
   sleep error.rate_limit.reset_in + 1
   retry
end
fffelix commented 6 years ago

Sorry, forgot to include the response:

"HTTP/1.1 420 Enhance Your Calm\r\nconnection: close\r\ncontent-length: 36\r\ncontent-type: text/html\r\ndate: Thu, 28 Jun 2018 10:36:18 GMT\r\nserver: tsa\r\nx-connection-hash: [HASH]\r\n\r\n" "Exceeded connection limit for user\r\n"

ERRORS doesn't include the 420 rate limit error. Right now I've added it in my local error.rb, but I was wondering if there's a better way of handling the streaming api rate limit error.

mtomov commented 3 years ago

Hey @fffelix , I got exactly the same one. I guess it's been there for a while.

How did you patch the library to handle the that error? And is it not the case of just adding

420 => Twitter::Error::TooManyRequests,

here: https://github.com/sferik/twitter/blob/master/lib/twitter/error.rb#L85 ?

Thanks!

fffelix commented 3 years ago

Hey @mtomov, yes, that's exactly what I did to fix it. But I only needed it while I was testing my app (and constantly restarting it, which resulted in these 420 errors). Haven't used for quite some time though.

marckohlbrugge commented 2 years ago

Hey @mtomov, yes, that's exactly what I did to fix it. But I only needed it while I was testing my app (and constantly restarting it, which resulted in these 420 errors). Haven't used for quite some time though.

Would you mind sharing how you monkey-patched this?

I'm running into the same error in production. Not often, but frequent enough that I want to rescue it.