File Coverage

blib/lib/WebService/PayPal/PaymentsAdvanced/Error/HTTP.pm
Criterion Covered Total %
statement 17 17 100.0
branch 7 8 87.5
condition 5 6 83.3
subroutine 3 3 100.0
pod 1 1 100.0
total 33 35 94.2


line stmt bran cond sub pod time code
1             package WebService::PayPal::PaymentsAdvanced::Error::HTTP;
2              
3 8     8   63 use Moo;
  8         25  
  8         57  
4              
5 8     8   2617 use namespace::autoclean;
  8         25  
  8         81  
6              
7             our $VERSION = '0.000026';
8              
9             extends 'Throwable::Error';
10              
11             with 'WebService::PayPal::PaymentsAdvanced::Error::Role::HasHTTPResponse';
12              
13             sub throw_from_http_response {
14 6     6 1 38 my $self = shift;
15 6         29 my %args = @_;
16              
17 6         16 my $response = $args{http_response};
18              
19 6 50       21 die q{"http_response" parameter is required} unless $response;
20              
21 6   100     31 my $message = delete $args{message_prefix} || q{};
22              
23             # LWP::UA doesn't throw exceptions or have sane error handling. It just
24             # sticks things in random, sometimes-undocumented headers and passes back
25             # a fake response.
26 6 100 100     24 if ( ( $response->header('Client-Warning') || q{} ) eq
    100          
    100          
27             'Internal response' ) {
28 1   50     58 $message
29             .= 'User-agent internal error: ' . ( $response->content || q{} );
30             }
31             elsif ( my $died_header = $response->header('X-Died') ) {
32 1         105 $message .= 'User-agent died: ' . $died_header;
33             }
34             elsif ( my $aborted_header = $response->header('Client-Aborted') ) {
35 1         151 $message .= 'User-agent aborted: ' . $aborted_header;
36             }
37             else {
38             # Given none of the above were set, this _might_ be a real HTTP
39             # error or one of several fake LWP errors where it doesn't set any
40             # particular header to indicate that it is a fake response.
41 3         506 $message
42             .= 'HTTP error ('
43             . $response->code . '): '
44             . $response->decoded_content;
45             }
46 6         1166 $self->throw(
47             message => $message,
48             http_status => $response->code,
49             %args
50             );
51             }
52              
53             1;
54              
55             # ABSTRACT: An HTTP transport error
56              
57             __END__
58              
59             =pod
60              
61             =head1 NAME
62              
63             WebService::PayPal::PaymentsAdvanced::Error::HTTP - An HTTP transport error
64              
65             =head1 VERSION
66              
67             version 0.000026
68              
69             =head1 SYNOPSIS
70              
71             use Try::Tiny;
72             use WebService::PayPal::PaymentsAdvanced;
73              
74             my $payments = WebService::PayPal::PaymentsAdvanced->new(
75             validate_hosted_form_uri => 1, ... );
76             my $response;
77              
78             my $uri;
79             try {
80             $response = $payments->create_secure_token(...);
81             }
82             catch {
83             die $_ unless blessed $_;
84             if ( $_->isa('WebService::PayPal::PaymentsAdvanced::Error::HTTP') ) {
85             log_http_error(
86             message => $_->message,
87             response_code => $_->http_status,
88             http_content => $_->http_response->content,
89             );
90             }
91              
92             # handle other exceptions
93             };
94              
95             =head1 DESCRIPTION
96              
97             This class represents an HTTP transport error.
98              
99             It extends L<Throwable::Error> and adds one attribute of its own.
100              
101             =head1 METHODS
102              
103             The C<< $error->message() >>, and C<< $error->stack_trace() >> methods are
104             inherited from L<Throwable::Error>.
105              
106             =head2 WebService::PayPal::PaymentsAdvanced::Error::HTTP->throw_from_http_response
107              
108             Throw a new instance of this class with a message created from the
109             required C<http_response> parameter. If the optional C<message_prefix> is
110             passed, the prefix will appear at the beginning of the message.
111              
112             =head2 $ex->http_response
113              
114             Returns the L<HTTP::Response> object which was returned when attempting the
115             HTTP request.
116              
117             =head2 $ex->http_status
118              
119             Returns the HTTP status code for the response.
120              
121             =head2 request_uri
122              
123             The URI of the request that caused the HTTP error.
124              
125             =head1 AUTHOR
126              
127             Olaf Alders <olaf@wundercounter.com>
128              
129             =head1 COPYRIGHT AND LICENSE
130              
131             This software is copyright (c) 2020 by MaxMind, Inc.
132              
133             This is free software; you can redistribute it and/or modify it under
134             the same terms as the Perl 5 programming language system itself.
135              
136             =cut