linvi / tweetinvi

Tweetinvi, an intuitive Twitter C# library for the REST and Stream API. It supports .NET, .NETCore, UAP (Xamarin)...
MIT License
1.01k stars 219 forks source link

TwitterException not being thrown for rate limit exceptions #1088

Closed shepherd324 closed 4 years ago

shepherd324 commented 4 years ago

I am running GetFriendsIterator queries in a loop and upon hitting my rate limit a 429 exception is being thrown, however my catch handler for the TwitterException is not being hit. My understanding is that a TwitterException should be thrown in the event that rate limits are hit.

try {
    var friendsIterator = userClient.Users.GetFriendsIterator(new GetFriendsParameters(handle));
    ...
}
catch(TwitterException e) {
    //this does not get hit
}
catch(Exception e) {
    //this gets hit
}
linvi commented 4 years ago

This is interesting. You should receive a TwitterException as the next operation should fail. I am assuming you are not using a RateLimit handler at the moment.

Would you mind sharing the exception that you are receiving?

linvi commented 4 years ago

I have actually tested this code and I did receive a TwitterException with 429 status code. Do we agree that that the code where you put ... is failing because of Tweetinvi?

This is the code you should be using.

while (!friendsIterator.Completed)
{
       await friendsIterator.NextPageAsync();
}
shepherd324 commented 4 years ago

Some additional details:

Here is the additional code in the ... block:

try {
    var friendsIterator = userClient.Users.GetFriendsIterator(new GetFriendsParameters(handle));
    while (!friendsIterator.Completed)
    {
        var page = friendsIterator.NextPageAsync().Result;
        foreach(var item in page.Items)
        {
            //do other things, no additional Tweetinvi calls
        }
    }
}
catch(TwitterException e) {
    //this does not get hit
}
catch(Exception e) {
    //this gets hit
}

Here is the full exception I am getting:

"System.AggregateException: One or more errors occurred. (Reason : Too Many Requests Details : Rate limit exceeded (88) Code : 429 Date : 11/24/2020 1:59:01 PM -06:00 URL : https://api.twitter.com/1.1/friends/ids.json?screen_name=xxx&count=5000 Twitter documentation description : Too Many Requests - Returned in API v1.1 when a request cannot be served due to the application's rate limit having been exhausted for the resource. See Rate Limiting in API v1.1. ) ---> Reason : Too Many Requests Details : Rate limit exceeded (88) Code : 429 Date : 11/24/2020 1:59:01 PM -06:00 URL : https://api.twitter.com/1.1/friends/ids.json?screen_name=xxx&count=5000 Twitter documentation description : Too Many Requests - Returned in API v1.1 when a request cannot be served due to the application's rate limit having been exhausted for the resource. See Rate Limiting in API v1.1.

--- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExcept ional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at GMRInfluencerFinder.Program.Run() in //Program.cs

linvi commented 4 years ago

Thank you for sharing your code. The issues comes from your .Result. async operations expect await to be used for exception handling.

If you wish to use .Result you will need to use the following code:

catch (AggregateException e)
{
    if (e.InnerException is TwitterException twitterException)
    {
        System.Console.WriteLine(twitterException.Message);
    }
}

Additionally I would highly suggest you use await to prevent deadlocks in your application.