line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Flotum::API::ExceptionHandler; |
2
|
4
|
|
|
4
|
|
28
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
115
|
|
3
|
4
|
|
|
4
|
|
18
|
use warnings; |
|
4
|
|
|
|
|
17
|
|
|
4
|
|
|
|
|
98
|
|
4
|
4
|
|
|
4
|
|
18
|
use utf8; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
24
|
|
5
|
4
|
|
|
4
|
|
110
|
use JSON::MaybeXS; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
2792
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
our @EXPORT = qw/request_with_retries/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub request_with_retries { |
12
|
16
|
|
|
16
|
0
|
346
|
my (%opts) = @_; |
13
|
16
|
|
|
|
|
45
|
my $logger = $opts{logger}; |
14
|
16
|
|
|
|
|
39
|
my $requester = $opts{requester}; |
15
|
16
|
|
50
|
|
|
90
|
my $tries = $opts{tries} || 3; |
16
|
16
|
|
50
|
|
|
74
|
my $sleep = $opts{sleep} || 1; |
17
|
16
|
|
|
|
|
42
|
my $name = $opts{name}; |
18
|
|
|
|
|
|
|
|
19
|
16
|
|
|
|
|
33
|
my ( $obj, $req, $res ); |
20
|
16
|
|
|
|
|
55
|
while ( $tries-- ) { |
21
|
|
|
|
|
|
|
|
22
|
16
|
|
|
|
|
34
|
my $func = $opts{method}; |
23
|
16
|
|
|
|
|
29
|
$obj = eval { |
24
|
|
|
|
|
|
|
$requester->stash->$func( |
25
|
16
|
|
|
|
|
298
|
@{ $opts{params} }, |
26
|
|
|
|
|
|
|
process_response => sub { |
27
|
16
|
|
|
16
|
|
6068424
|
$res = $_[0]->{res}; |
28
|
16
|
|
|
|
|
67
|
$req = $_[0]->{req}; |
29
|
|
|
|
|
|
|
}, |
30
|
16
|
|
|
|
|
320
|
); |
31
|
|
|
|
|
|
|
}; |
32
|
16
|
100
|
|
|
|
2188
|
last unless $@; |
33
|
|
|
|
|
|
|
|
34
|
1
|
50
|
|
|
|
5
|
die "Response not defined: $@" unless defined $res; |
35
|
1
|
50
|
33
|
|
|
5
|
if ( $res->code == 404 && $res->content !~ /Endpoint not found/ ) { |
36
|
1
|
|
|
|
|
51
|
die "Resource does not exists\n"; |
37
|
|
|
|
|
|
|
} |
38
|
0
|
0
|
0
|
|
|
0
|
if ( $res->code == 400 && ref $obj eq 'HASH' && ref $obj->{error} eq 'form_error' ) { |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
39
|
0
|
|
|
|
|
0
|
my $msg = "Invalid data:\n"; |
40
|
0
|
|
|
|
|
0
|
$msg .= "$_ = " . $obj->{form_error}{$_} . "\n" for keys %{ $obj->{form_error} }; |
|
0
|
|
|
|
|
0
|
|
41
|
0
|
|
|
|
|
0
|
$logger->error( &log_error_txt( $@, $req, $res ) ); |
42
|
0
|
|
|
|
|
0
|
$logger->error($msg); |
43
|
0
|
|
|
|
|
0
|
die "$msg\n"; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
elsif ( $res->code == 400 && ref $obj eq 'ARRAY' ) { |
46
|
0
|
|
|
|
|
0
|
my $msg = "Invalid data:\n"; |
47
|
0
|
|
|
|
|
0
|
$msg .= encode_json($_) . "\n" for @{$obj}; |
|
0
|
|
|
|
|
0
|
|
48
|
0
|
|
|
|
|
0
|
$logger->error( &log_error_txt( $@, $req, $res ) ); |
49
|
0
|
|
|
|
|
0
|
$logger->error($msg); |
50
|
0
|
|
|
|
|
0
|
die $obj; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
0
|
$logger->error( &log_error_txt( $@, $req, $res ) ); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# erros nao 500 desiste na hora. |
56
|
0
|
0
|
0
|
|
|
0
|
if ( $tries == 0 || $res->code != 500 ) { |
57
|
0
|
|
|
|
|
0
|
$logger->error( "Giving up $name. Reponse code " . $res->code ); |
58
|
0
|
|
|
|
|
0
|
die "Can't $name right now, response code ${\$res->code}.\n"; |
|
0
|
|
|
|
|
0
|
|
59
|
|
|
|
|
|
|
} |
60
|
0
|
|
|
|
|
0
|
$logger->info("trying $tries more times..."); |
61
|
0
|
|
|
|
|
0
|
sleep $sleep; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
15
|
|
|
|
|
276
|
return ( obj => $obj, res => $res ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub log_error_txt { |
68
|
0
|
|
|
0
|
0
|
|
my ( $err, $req, $res ) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return "Error! $err\nREQUEST: \n" . eval { $req->as_string } . "\nRESPONSE\n" . eval { $res->as_string }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |