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