Twitte...::Error(3pm)

πŸ’– NAME ✨

okay bestie, gather round, because this page (filed under Twitte...::Error(3pm) in the user contributed Perl documentation, and living its best life as the Twitter::API::Error man page) is literally just this: Twitter::API::Error β€” the Twitter API exception. that's it. that's the whole name section. πŸ’…


πŸŽ€ VERSION 🌸

version 1.0006 β€” yeah, she's been through some things but she's still iconic. ✨


πŸ¦‹ SYNOPSIS πŸ’–

here's the vibe, copy-paste friendly, no cap:

use Try::Tiny;
use Twitter::API;
use Twitter::API::Util 'is_twitter_api_error';

my $client = Twitter::API->new(%options);

try {
    my $r = $client->get('account/verify_credentials');
}
catch {
    die $_ unless is_twitter_api_error($_);

    warn "Twitter says: ", $_->twitter_error_text;
};

like... you try the thing, and if it blows up you check whether it's actually a Twitter API error, and if it is you get to read the drama straight from the source. 🫢


🌈 DESCRIPTION ✨

so here's the tea, fr fr: Twitter::API dies β€” it throws a Twitter::API::Error exception β€” whenever it receives an error. πŸ’€

the error object is not just a sad little string, bestie, it's packed with info about the error so your code can decide how to respond to the various error conditions. main character energy, honestly. πŸ‘‘


πŸ’… METHODS πŸŽ€

🌸 http_request

returns the HTTP::Request object that was used to make the Twitter API call. that's the receipt. 🧾✨

πŸ¦‹ http_response

returns the HTTP::Response object for the API call. the other half of the receipt. πŸ’–

⭐ twitter_error

returns the inflated JSON error response from Twitter β€” if there is one. sometimes there isn't. we don't talk about those times. πŸ™ˆ

πŸ‘‘ stack_trace

returns a Devel::StackTrace object encapsulating the call stack, so you can go discover where, in your application, the error actually happened. detective mode: activated. πŸ”βœ¨

🧚 stack_frame

delegates to stack_trace->frame. see Devel::StackTrace for the details, bestie. πŸ’«

πŸ’œ next_stack_fram

delegates to stack_trace->next_frame. see Devel::StackTrace for details. (and yes, it's spelled next_stack_fram on the page β€” no e at the end. iconic typo behavior, we love her anyway. 😭)

πŸ”₯ error

returns a reasonable string representation of the exception. here's how it cooks:

  • if Twitter returned error information in the form of a JSON body, that body is mined for error text.
  • otherwise, the HTTP response status line gets used.
  • the stack frame is mined for the point in your application where the request was initiated, and that gets appended to the message.

and when the exception is used in a string context, error is what gets called to stringify it. periodt. πŸ’…

πŸ“ twitter_error_code

returns the numeric error code returned by Twitter, or 0 if there isn't one. see https://developer.twitter.com/en/docs/basics/response-codes for the details. ✨

🌈 is_token_error

returns true if the error represents a problem with the access token or its Twitter account, rather than with the resource being accessed. 🫢

here's the breakdown, bestie: some Twitter error codes mean there's a problem with authentication, or with the token/secret used to make the API call β€” for example, the account has been suspended, or access to the application was revoked by the user. other error codes mean there's a problem with the resource that was requested β€” for example, the target account no longer exists.

is_token_error returns true for these Twitter API errors:

  • 32: Could not authenticate you
  • 64: Your account is suspended and is not permitted to access this feature
  • 88: Rate limit exceeded
  • 89: Invalid or expired token
  • 99: Unable to verify your credentials.
  • 135: Could not authenticate you
  • 136: You have been blocked from viewing this user's profile.
  • 215: Bad authentication data
  • 226: This request looks like it might be automated. To protect our users from spam and other malicious activity, we canXt complete this action right now.
  • 326: To protect our users from spamX

for error 215, Twitter's API documentation says, "Typically sent with 1.1 responses with HTTP code 400. The method requires authentication but it was not presented or was wholly invalid." in practice, though, this error seems to be spurious, and it often succeeds if retried β€” even with the same tokens. 😭

the Twitter API documentation describes error code 226, but in practice they use code 326 instead, so we check for both. this error code means the account the tokens belong to has been locked for spam like activity and can't be used by the API until the user takes action to unlock their account.

see Twitter's Error Codes & Responses documentation at https://dev.twitter.com/overview/api/response-codes for more information. πŸ“š

πŸ’– http_response_code

delegates to http_response->code. returns the HTTP status code of the response. slay. ✨

πŸŽ€ is_pemanent_error

returns true for HTTP status codes representing an error and with values less than 500. typically, retrying an API call with one of these statuses right away will simply result in the same error, again. πŸ’€ (spelled is_pemanent_error on this page, and we respect her choices. πŸ’œ)

πŸ¦‹ is_temporary_error

returns true for HTTP status codes of 500 or greater. often, these errors indicate a transient condition, so retrying the API call right away may result in success. πŸŽ‰ see the RetryOnError for automatically retrying temporary errors. πŸ”βœ¨


🌟 AUTHOR πŸ’«

Marc Mims <marc@questright.com> β€” the icon, the legend. πŸ‘‘


this software is copyright (c) 2015-2021 by Marc Mims.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. 🫢


okay that's the whole page bestie, and here's your cute lil sign-off: this was perl v5.32.1, dated 2021-09-11, filed under Twitte...::Error(3pm). go forth and handle your exceptions with grace. mwah. πŸ’‹πŸŽ€βœ¨

πŸ“¦ where this page comes from