semifor / Net-Twitter

A Perl interface to the Twitter APIs
83 stars 37 forks source link

lookup_users does not catch 404 for wrong user ids #12

Closed towo closed 11 years ago

towo commented 11 years ago

If you pass a non-existent user_id to lookup_users, e.g. $twitter->lookup_users({user_id => '123456'}), and there is no valid ID in the request, Net::Twitter will throw a 404 on STDERR and act as if the user did something wrong.

The Twitter API consistently sends a 404 with a 'page not found' message when there's no valid users. This should, in this special case, be catched and processed, returning an empty array, undef or similar, so that scripts relying on probing for info don't have to play catch().

semifor commented 11 years ago

In general, I like to pass Twitter errors through. This may be a reasonable request. I'm considering it.

lookup_users isn't entirely reliable. You can get fewer results than you request, and the missing users may be suspended, deleted, or may have never existed. But that isn't always true. A 404 response may be a more reliable indicator that the users requested are, indeed, gone.

In my own code, I take the 404 to mean the requested users are gone. I fall back to show_user calls or missing results to verify.

Also note, that at the moment Twitter has a serious problem with lookup_users. They are handling the call as an unauthenticated request and when the rate limit is exhausted, they return a 401 rather than the documented 429.

See: 401 on rate limit, and can't get X-RateLimit-Class api-identified

towo commented 11 years ago

Well, that's perfectly fine, I'd say - but what confused me (hell, maybe I'm just having a slowpoke moment) is that the 404 doesn't get returned but rather dumped out via STDERR. Feels a bit non-ideal to have to specifically catch that instead of merely parsing the return value.

semifor commented 11 years ago

That's the default behavior for an exception that isn't caught. I recommend using Try::Tiny, and catch-ing the error. If you prefer an undef return on error, you can use the WrapError trait in new, then check $nt->get_error on undef.

my $nt = Net::Twitter->new(traits => [qw/API::REST WrapError/], ...);
my $r = $nt->lookup_users(...);
if ( $r ) {
    # process results
}
else {
    # handle, log, or ignore the error per your requirements
    warn $nt->get_error;
}

I much prefer the errors as exception model, myself.

If you're actually get the error to STDERR instead of an exception thrown, please show me some code so I can investigate it and get a fix. Feel free to email me. My address is in the docs: peroldoc Net::Twitter.

towo commented 11 years ago

Ah, okay, I was actually having a brainfart here, it's an exception, of course. Thanks for helping me unclog myself! :)

[For the record of ze Googles: the exception caught for a no-results lookup_users will be Sorry, that page does not exist at <file> line <line>]