File Coverage

blib/lib/WWW/Tumblr/ResponseError.pm
Criterion Covered Total %
statement 9 42 21.4
branch 0 24 0.0
condition 0 28 0.0
subroutine 3 6 50.0
pod 2 3 66.6
total 14 103 13.5


line stmt bran cond sub pod time code
1             package WWW::Tumblr::ResponseError;
2              
3 16     16   143 use Moose;
  16         34  
  16         146  
4 16     16   145185 use Data::Dumper;
  16         40  
  16         1627  
5 16     16   128 use JSON 'decode_json';
  16         37  
  16         164  
6              
7             has 'response', is => 'rw', isa => 'HTTP::Response';
8              
9 0     0 1   sub code { $_[0]->response->code }
10              
11             sub is_rate_limited {
12 0     0 0   my $self = shift;
13             # Check for HTTP 429 (Too Many Requests) or 400 with rate limit error code
14 0 0         return 1 if $self->code == 429;
15            
16 0 0         if ($self->code == 400) {
17 0           my $content = $self->response->decoded_content;
18 0           my $j;
19 0           eval { $j = JSON::decode_json($content); };
  0            
20 0 0         return 0 if $@;
21            
22             # Check for Tumblr's rate limit error code 8004
23 0 0 0       if (ref $j eq 'HASH' && ref $j->{response} eq 'HASH' &&
      0        
24             ref $j->{response}{errors} eq 'ARRAY') {
25 0           for my $err (@{$j->{response}{errors}}) {
  0            
26 0 0 0       return 1 if ref $err eq 'HASH' && ($err->{code} || 0) == 8004;
      0        
27             }
28             }
29             }
30 0           return 0;
31             }
32             sub reasons {
33 0     0 1   my $self = $_[0];
34 0           my $content = $_[0]->response->decoded_content;
35 0           my $j;
36 0           eval { $j = decode_json($content); };
  0            
37 0 0         if ($@) {
38             # Response is not valid JSON, return HTTP message
39 0   0       return [ $self->response->message || 'Unknown error' ];
40             }
41 0 0 0       if ( ref $j && ref $j eq 'HASH' ) {
42 0 0 0       if ( ref $j->{response} && ref $j->{response} eq 'ARRAY' ) {
    0 0        
      0        
43 0 0         unless ( scalar @{ $j->{response} }) {
  0            
44 0           return [ $self->response->message ]
45             }
46 0           return $j->{response};
47             } elsif ( ref $j->{response} && ref $j->{response} eq 'HASH' &&
48             defined $j->{response}->{errors}
49             ) {
50 0 0 0       if ( ref $j->{response}->{errors} eq 'HASH' &&
    0          
51             defined $j->{response}->{errors}->{state} ) {
52             return [
53             $j->{response}->{errors}->{0},
54             $j->{response}->{errors}->{state}
55 0           ];
56             } elsif ( ref $j->{response}->{errors} eq 'ARRAY' ) {
57 0           return $j->{response}->{errors};
58             } else {
59 0           Carp::croak "Unimplemented";
60             }
61             } else {
62 0           Carp::croak "Unimplemented";
63             }
64             } else {
65 0           Carp::croak "Unimplemented";
66             }
67             }
68              
69             1;
70              
71             =pod
72              
73             =head1 NAME
74              
75             WWW::Tumblr::ResponseError
76              
77             =head1 SYNOPSIS
78              
79             my $posts = $tumblr->blog('stupidshit.tumblr.com')->posts;
80              
81             die "Couldn't get posts! " . Dumper( $tumblr->error->reasons ) unless $posts;
82              
83             =head1 DESCRIPTION
84              
85             This a class representing L<WWW::Tumblr>'s C<error> method. It contains the
86             response from upstream Tumblr API.
87              
88             =head1 METHODS
89              
90             =head2 error
91              
92             Callable from a model context, usually L<WWW::Tumblr>.
93              
94             print Dumper $tumblr->error unless $post;
95              
96             =head2 code
97              
98             HTTP response code for the error:
99              
100             my $info = $blog->info;
101             print $blog->error->code . ' nono :(' unless $info;
102              
103             =head2 reasons
104              
105             Commodity method to display reasons why the error ocurred. It returns an array
106             reference:
107              
108             unless ( $some_tumblr_action ) {
109             print "Errors! \n";
110             print $_, "\n" for @{ $tumblr->error->reasons || [] };
111             }
112              
113             =head1 BUGS
114              
115             Please refer to L<WWW::Tumblr>.
116              
117             =head1 AUTHOR(S)
118              
119             The same folks as L<WWW::Tumblr>.
120              
121             =head1 SEE ALSO
122              
123             L<WWW::Tumblr>, L<WWW::Tumblr::ResponseError>.
124              
125             =head1 COPYRIGHT and LICENSE
126              
127             Same as L<WWW::Tumblr>.
128              
129             =cut
130