The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Twitter::API::Error - Twitter API exception

VERSION

version 0.0111

SYNOPSIS

    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;
    };

DESCRIPTION

Twitter::API dies, throwing a Twitter::API::Error exception when it receives an error. The error object contains information about the error so your code can decide how to respond to various error conditions.

METHODS

http_request

Returns the HTTP::Request object used to make the Twitter API call.

http_response

Returns the HTTP::Response object for the API call.

twitter_error

Returns the inflated JSON error response from Twitter (if any).

stack_trace

Returns a Devel::StackTrace object encapsulating the call stack so you can discover, where, in your application the error occurred.

stack_frame

Delegates to stack_trace->frame. See Devel::StackTrace for details.

next_stack_fram

Delegates to stack_trace->next_frame. See Devel::StackTrace for details.

error

Returns a reasonable string representation of the exception. If Twitter returned error information in the form of a JSON body, it is mined for error text. Otherwise, the HTTP response status line is used. The stack frame is mined for the point in your application where the request initiated and appended to the message.

When used in a string context, error is called to stringify exception.

twitter_error_code

Returns the numeric error code returned by Twitter, or 0 if there is none. See https://dev.twitter.com/overview/api/response-codes for 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.

Some Twitter error codes indicate a problem with authentication or the token/secret used to make the API call. For example, the account has been suspended or access to the application revoked by the user. Other error codes indicate a problem with the resource requested. For example, the target account no longer exists.

is_token_error returns true for the following 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 can’t complete this action right now.

  • 326: To protect our users from spam…

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 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 for more information.

http_response_code

Delegates to http_response->code. Returns the HTTP status code of the response.

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.

is_temporary_error

Returns true or HTTP status codes of 500 or greater. Often, these errors indicate a transient condition. Retrying the API call right away may result in success. See the RetryOnError for automatically retrying temporary errors.

AUTHOR

Marc Mims <marc@questright.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015-2016 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.