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   72 use Moo;
  8         24  
  8         65  
4              
5 8     8   2733 use namespace::autoclean;
  8         33  
  8         71  
6              
7             our $VERSION = '0.000028';
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 27 my $self = shift;
15 6         25 my %args = @_;
16              
17 6         15 my $response = $args{http_response};
18              
19 6 50       21 die q{"http_response" parameter is required} unless $response;
20              
21 6   100     28 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     26 if ( ( $response->header('Client-Warning') || q{} ) eq
    100          
    100          
27             'Internal response' ) {
28 1   50     60 $message
29             .= 'User-agent internal error: ' . ( $response->content || q{} );
30             }
31             elsif ( my $died_header = $response->header('X-Died') ) {
32 1         106 $message .= 'User-agent died: ' . $died_header;
33             }
34             elsif ( my $aborted_header = $response->header('Client-Aborted') ) {
35 1         152 $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         549 $message
42             .= 'HTTP error ('
43             . $response->code . '): '
44             . $response->decoded_content;
45             }
46 6         1140 $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             =encoding UTF-8
62              
63             =head1 NAME
64              
65             WebService::PayPal::PaymentsAdvanced::Error::HTTP - An HTTP transport error
66              
67             =head1 VERSION
68              
69             version 0.000028
70              
71             =head1 SYNOPSIS
72              
73             use Try::Tiny;
74             use WebService::PayPal::PaymentsAdvanced;
75              
76             my $payments = WebService::PayPal::PaymentsAdvanced->new(
77             validate_hosted_form_uri => 1, ... );
78             my $response;
79              
80             my $uri;
81             try {
82             $response = $payments->create_secure_token(...);
83             }
84             catch {
85             die $_ unless blessed $_;
86             if ( $_->isa('WebService::PayPal::PaymentsAdvanced::Error::HTTP') ) {
87             log_http_error(
88             message => $_->message,
89             response_code => $_->http_status,
90             http_content => $_->http_response->content,
91             );
92             }
93              
94             # handle other exceptions
95             };
96              
97             =head1 DESCRIPTION
98              
99             This class represents an HTTP transport error.
100              
101             It extends L<Throwable::Error> and adds one attribute of its own.
102              
103             =head1 METHODS
104              
105             The C<< $error->message() >>, and C<< $error->stack_trace() >> methods are
106             inherited from L<Throwable::Error>.
107              
108             =head2 WebService::PayPal::PaymentsAdvanced::Error::HTTP->throw_from_http_response
109              
110             Throw a new instance of this class with a message created from the
111             required C<http_response> parameter. If the optional C<message_prefix> is
112             passed, the prefix will appear at the beginning of the message.
113              
114             =head2 $ex->http_response
115              
116             Returns the L<HTTP::Response> object which was returned when attempting the
117             HTTP request.
118              
119             =head2 $ex->http_status
120              
121             Returns the HTTP status code for the response.
122              
123             =head2 request_uri
124              
125             The URI of the request that caused the HTTP error.
126              
127             =head1 SUPPORT
128              
129             Bugs may be submitted through L<https://github.com/maxmind/webservice-paypal-paymentsadvanced/issues>.
130              
131             =head1 AUTHOR
132              
133             Olaf Alders <olaf@wundercounter.com>
134              
135             =head1 COPYRIGHT AND LICENSE
136              
137             This software is copyright (c) 2022 by MaxMind, Inc.
138              
139             This is free software; you can redistribute it and/or modify it under
140             the same terms as the Perl 5 programming language system itself.
141              
142             =cut