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 you64: Your account is suspended and is not permitted to access this feature88: Rate limit exceeded89: Invalid or expired token99: Unable to verify your credentials.135: Could not authenticate you136: You have been blocked from viewing this user's profile.215: Bad authentication data226: 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. π
π COPYRIGHT AND LICENSE π
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
- debian bookworm: libtwitter-api-perl 1.0006-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtwitter-api-perl 1.0006-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libtwitter-api-perl 1.0006-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtwitter-api-perl 1.0006-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtwitter-api-perl 1.0006-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtwitter-api-perl 1.0006-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms