1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
package Twitter::API::Error;
# ABSTRACT: Twitter API exception
$Twitter::API::Error::VERSION = '1.0006';
use Moo;
use Ref::Util qw/is_arrayref is_hashref/;
use Try::Tiny;
use namespace::clean;
use overload '""' => sub { shift->error };
with qw/Throwable StackTrace::Auto/;
#pod =method http_request
#pod
#pod Returns the L<HTTP::Request> object used to make the Twitter API call.
#pod
#pod =method http_response
#pod
#pod Returns the L<HTTP::Response> object for the API call.
#pod
#pod =method twitter_error
#pod
#pod Returns the inflated JSON error response from Twitter (if any).
#pod
#pod =cut
has context => (
is => 'ro',
required => 1,
handles => {
http_request => 'http_request',
http_response => 'http_response',
twitter_error => 'result',
},
);
#pod =method stack_trace
#pod
#pod Returns a L<Devel::StackTrace> object encapsulating the call stack so you can discover, where, in your application the error occurred.
#pod
#pod =method stack_frame
#pod
#pod Delegates to C<< stack_trace->frame >>. See L<Devel::StackTrace> for details.
#pod
#pod =method next_stack_fram
#pod
#pod Delegates to C<< stack_trace->next_frame >>. See L<Devel::StackTrace> for details.
#pod
#pod =cut
has '+stack_trace' => (
handles => {
stack_frame => 'frame',
next_stack_frame => 'next_frame',
},
);
#pod =method error
#pod
#pod Returns a reasonable string representation of the exception. If Twitter
#pod returned error information in the form of a JSON body, it is mined for error
#pod text. Otherwise, the HTTP response status line is used. The stack frame is
#pod mined for the point in your application where the request initiated and
#pod appended to the message.
#pod
#pod When used in a string context, C<error> is called to stringify exception.
#pod
#pod =cut
has error => (
is => 'lazy',
);
sub _build_error {
my $self = shift;
my $error = $self->twitter_error_text || $self->http_response->status_line;
my ( $location ) = $self->stack_frame(0)->as_string =~ /( at .*)/;
return $error . ($location || '');
}
sub twitter_error_text {
my $self = shift;
# Twitter does not return a consistent error structure, so we have to
# try each known (or guessed) variant to find a suitable message...
return '' unless $self->twitter_error;
my $e = $self->twitter_error;
return is_hashref($e) && (
# the newest variant: array of errors
exists $e->{errors}
&& is_arrayref($e->{errors})
&& exists $e->{errors}[0]
&& is_hashref($e->{errors}[0])
&& exists $e->{errors}[0]{message}
&& $e->{errors}[0]{message}
# it's single error variant
|| exists $e->{error}
&& is_hashref($e->{error})
&& exists $e->{error}{message}
&& $e->{error}{message}
# the original error structure (still applies to some endpoints)
|| exists $e->{error} && $e->{error}
# or maybe it's not that deep (documentation would be helpful, here,
# Twitter!)
|| exists $e->{message} && $e->{message}
) || ''; # punt
}
#pod =method twitter_error_code
#pod
#pod Returns the numeric error code returned by Twitter, or 0 if there is none. See
#pod L<https://developer.twitter.com/en/docs/basics/response-codes> for details.
#pod
#pod =cut
sub twitter_error_code {
my $self = shift;
for ( $self->twitter_error ) {
return is_hashref($_)
&& exists $_->{errors}
&& exists $_->{errors}[0]
&& exists $_->{errors}[0]{code}
&& $_->{errors}[0]{code}
|| 0;
}
}
#pod =method is_token_error
#pod
#pod Returns true if the error represents a problem with the access token or its
#pod Twitter account, rather than with the resource being accessed.
#pod
#pod Some Twitter error codes indicate a problem with authentication or the
#pod token/secret used to make the API call. For example, the account has been
#pod suspended or access to the application revoked by the user. Other error codes
#pod indicate a problem with the resource requested. For example, the target account
#pod no longer exists.
#pod
#pod is_token_error returns true for the following Twitter API errors:
#pod
#pod =for :list
#pod * 32: Could not authenticate you
#pod * 64: Your account is suspended and is not permitted to access this feature
#pod * 88: Rate limit exceeded
#pod * 89: Invalid or expired token
#pod * 99: Unable to verify your credentials.
#pod * 135: Could not authenticate you
#pod * 136: You have been blocked from viewing this user's profile.
#pod * 215: Bad authentication data
#pod * 226: This request looks like it might be automated. To protect our users from
#pod spam and other malicious activity, we can’t complete this action right now.
#pod * 326: To protect our users from spam…
#pod
#pod For error 215, Twitter's API documentation says, "Typically sent with 1.1
#pod responses with HTTP code 400. The method requires authentication but it was not
#pod presented or was wholly invalid." In practice, though, this error seems to be
#pod spurious, and often succeeds if retried, even with the same tokens.
#pod
#pod The Twitter API documentation describes error code 226, but in practice, they
#pod use code 326 instead, so we check for both. This error code means the account
#pod the tokens belong to has been locked for spam like activity and can't be used
#pod by the API until the user takes action to unlock their account.
#pod
#pod See Twitter's L<Error Codes &
#pod Responses|https://dev.twitter.com/overview/api/response-codes> documentation
#pod for more information.
#pod
#pod =cut
use constant TOKEN_ERRORS => (32, 64, 88, 89, 99, 135, 136, 215, 226, 326);
my %token_errors = map +($_ => undef), TOKEN_ERRORS;
sub is_token_error {
exists $token_errors{shift->twitter_error_code};
}
#pod =method http_response_code
#pod
#pod Delegates to C<< http_response->code >>. Returns the HTTP status code of the
#pod response.
#pod
#pod =cut
sub http_response_code { shift->http_response->code }
#pod =method is_pemanent_error
#pod
#pod Returns true for HTTP status codes representing an error and with values less
#pod than 500. Typically, retrying an API call with one of these statuses right away
#pod will simply result in the same error, again.
#pod
#pod =cut
sub is_permanent_error { shift->http_response_code < 500 }
#pod =method is_temporary_error
#pod
#pod Returns true or HTTP status codes of 500 or greater. Often, these errors
#pod indicate a transient condition. Retrying the API call right away may result in
#pod success. See the L<RetryOnError|Twitter::API::Trait::RetryOnError> for
#pod automatically retrying temporary errors.
#pod
#pod =cut
sub is_temporary_error { !shift->is_permanent_error }
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Twitter::API::Error - Twitter API exception
=head1 VERSION
version 1.0006
=head1 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;
};
=head1 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.
=head1 METHODS
=head2 http_request
Returns the L<HTTP::Request> object used to make the Twitter API call.
=head2 http_response
Returns the L<HTTP::Response> object for the API call.
=head2 twitter_error
Returns the inflated JSON error response from Twitter (if any).
=head2 stack_trace
Returns a L<Devel::StackTrace> object encapsulating the call stack so you can discover, where, in your application the error occurred.
=head2 stack_frame
Delegates to C<< stack_trace->frame >>. See L<Devel::StackTrace> for details.
=head2 next_stack_fram
Delegates to C<< stack_trace->next_frame >>. See L<Devel::StackTrace> for details.
=head2 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, C<error> is called to stringify exception.
=head2 twitter_error_code
Returns the numeric error code returned by Twitter, or 0 if there is none. See
L<https://developer.twitter.com/en/docs/basics/response-codes> for details.
=head2 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:
=over 4
=item *
32: Could not authenticate you
=item *
64: Your account is suspended and is not permitted to access this feature
=item *
88: Rate limit exceeded
=item *
89: Invalid or expired token
=item *
99: Unable to verify your credentials.
=item *
135: Could not authenticate you
=item *
136: You have been blocked from viewing this user's profile.
=item *
215: Bad authentication data
=item *
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.
=item *
326: To protect our users from spam…
=back
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 L<Error Codes &
Responses|https://dev.twitter.com/overview/api/response-codes> documentation
for more information.
=head2 http_response_code
Delegates to C<< http_response->code >>. Returns the HTTP status code of the
response.
=head2 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.
=head2 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 L<RetryOnError|Twitter::API::Trait::RetryOnError> for
automatically retrying temporary errors.
=head1 AUTHOR
Marc Mims <marc@questright.com>
=head1 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.
=cut
|