File Coverage

blib/lib/Net/Twitter/Role/WrapError.pm
Criterion Covered Total %
statement 15 15 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 23 23 100.0


line stmt bran cond sub pod time code
1             package Net::Twitter::Role::WrapError;
2             $Net::Twitter::Role::WrapError::VERSION = '4.01043';
3 9     9   4182 use Moose::Role;
  9         19  
  9         52  
4 9     9   39687 use Try::Tiny;
  9         21  
  9         499  
5 9     9   50 use Scalar::Util qw/blessed/;
  9         18  
  9         469  
6              
7             requires qw/_parse_result/;
8              
9 9     9   60 use namespace::autoclean;
  9         27  
  9         72  
10              
11             has _http_response => ( isa => 'HTTP::Response', is => 'rw',
12                                     handles => {
13                                         http_message => 'message',
14                                         http_code => 'code',
15                                     }
16                                   );
17             has _twitter_error => ( isa => 'HashRef|Object', is => 'rw', predicate => 'has_error',
18                                     clearer => '_clear_error' );
19              
20             has _error_return_val => ( isa => 'Maybe[ArrayRef]', is => 'rw', default => undef );
21              
22             sub get_error {
23 7     7 1 1417     my $self = shift;
24              
25 7 100       261     return unless $self->has_error;
26              
27 5         123     return $self->_twitter_error;
28             }
29              
30             around _parse_result => sub {
31                 my ($next, $self, $res, $sythetic_args, $datetime_parser) = @_;
32              
33                 $self->_clear_error;
34                 $self->_http_response($res);
35              
36                 my $r = try { $next->($self, $res, $sythetic_args, $datetime_parser) }
37                 catch {
38                     die $_ unless blessed $_ && $_->isa('Net::Twitter::Error');
39              
40                     $self->_twitter_error($_->has_twitter_error
41                         ? $_->twitter_error
42                         : $self->_inflate_objects(
43                             $datetime_parser,
44                             {
45                               error => "TWITTER RETURNED ERROR MESSAGE BUT PARSING OF JSON RESPONSE FAILED - "
46                                      . $res->message
47                             }
48                           )
49                     );
50                     return $self->_error_return_val;
51                 };
52              
53                 return $r;
54             };
55              
56             1;
57              
58             __END__
59            
60             =head1 NAME
61            
62             Net::Twitter::Role::WrapError - Wraps Net::Twitter exceptions
63            
64             =head1 VERSION
65            
66             version 4.01043
67            
68             =head1 SYNOPSIS
69            
70             use Net::Twitter;
71            
72             my $nt = Net::Twitter->new(username => $username, password => $password);
73            
74             my $followers = $nt->followers;
75             if ( !followers ) {
76             warn $nt->http_message;
77             }
78            
79             =head1 DESCRIPTION
80            
81             This module provides an alternate error handling strategy for C<Net::Twitter>.
82             Rather than throwing exceptions, API methods return C<undef> and error
83             information is available through method calls on the C<Net::Twitter> object.
84            
85             This is the error handling strategy used when C<trait> C<Legacy> is used. It
86             was the error handling strategy employed by C<Net::Twitter> prior to version
87             3.00.
88            
89             =head1 METHODS
90            
91             =over 4
92            
93             =item new
94            
95             This method takes the same parameters as L<Net::Twitter/new>.
96            
97             =item get_error
98            
99             Returns undef if there was no error or a HASH ref containing the Twitter error
100             response. Occasionally, a Twitter API call results in an error with no error
101             content returned from Twitter. When that occurs, get_error returns a simulated
102             error HASH ref.
103            
104             NOTE: Versions of C<Net::Twitter> prior to 3.0 sometimes returned a string
105             rather than a HASH ref, on error. This was a bug. Always expect undef on
106             success and a HASH ref on error.
107            
108             =item http_code
109            
110             Returns the HTTP response code the most recent API method call if it ended in error.
111            
112             =item http_message
113            
114             Returns the HTTP message for the most recent API method call if it ended in error.
115            
116             =back
117            
118             =head1 SEE ALSO
119            
120             L<Net::Twitter>
121            
122            
123             =head1 AUTHOR
124            
125             Marc Mims <marc@questright.com>
126            
127             =head1 LICENSE
128            
129             Copyright (c) 2016 Marc Mims
130            
131             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
132            
133             =head1 DISCLAIMER OF WARRANTY
134            
135             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
136             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
137             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
138             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
139             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
140             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
141             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
142             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
143             NECESSARY SERVICING, REPAIR, OR CORRECTION.
144            
145             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
146             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
147             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE
148             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
149             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
150             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
151             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
152             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
153             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
154             SUCH DAMAGES.
155