VPNDetection/Error.pm

filter: Statementstmt 92.7% 77 / 83 Branchbran 84.0% 37 / 44 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 90.4% 19 / 21 Podpod 50.0% 5 / 10 totaltotal 84.8% 146 / 172 CC 27 SCAR 15.4
CC
27
Cov
88%
wrap
8.7
from_response
8.0
_http_date
6.0
CRAP 4.7
« VPNDetection/Oauth.pm ↑ Summary VPNDetection/Result.pm »
1 package VPNDetection::Error;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Mojo::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Time::Local ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use overload
11 0 ▶ '""' => sub { $_[0]->{message} },
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 126 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 7 fallback => 1;
14  
15 7 our $VERSION = '3.15.0';
16  
17 7 my %RETRYABLE = (rate_limited => 1, server_error => 1, network => 1);
18  
19 7 my %MONTH;
20 7 @MONTH{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)} = 0 .. 11;
21 7 my $DAY = join '|', qw(Mon Tue Wed Thu Fri Sat Sun);
22 7 my $WEEKDAY = join '|', qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday);
23 7 my $MON = join '|', keys %MONTH;
24 7 my $TIME = '([0-9]{2}):([0-9]{2}):([0-9]{2})';
25  
26 751 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
27 751 my ($class, %args) = @_;
28 return bless {
29 kind => $args{kind},
30 message => defined $args{message} ? $args{message} : $args{kind},
31 status => $args{status},
32 retry_after => $args{retry_after},
33 751 ▶ }, $class;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'message'} ? : 751 0 751
34 }
35  
36 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my $class = shift;
38 0 die $class->new(@_);
39 }
40  
41 # Classifies one failed response. The two APIs behind this host answer with
42 # different envelopes - the lookup endpoint uses `error`, the database endpoints
43 # use `rc` - and both are read here so a caller never has to know which was hit.
44 91 ▶sub from_response {
Subroutine from_response: called CC=8 SCAR=20.8
Pod undocumented
45 91 my ($class, $status, $headers, $body) = @_;
46 91 my $message = _message($body);
47 91 ▶ $message = "request failed with status $status" unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 13 78 91
48 91 my $retry_after = _retry_after($headers->header('Retry-After'));
49  
50 # Present means a transient rate limit and retrying works; absent means an
51 # allowance is spent. Nothing else in the response separates the two.
52 91 ▶ if ($status == 429) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 429) 19 72 91
53 19 ▶ return $class->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $retry_after ? : 9 10 19
54 kind => defined $retry_after ? 'rate_limited' : 'quota_exceeded',
55 message => $message, status => $status, retry_after => $retry_after,
56 );
57 }
58 72 ▶ return $class->new(kind => 'bad_request', message => $message, status => $status)
Branch: 1 branch
BranchTrueFalseTotal
if $status == 400 28 44 72
59 if $status == 400;
60 44 ▶ return $class->new(kind => 'unauthorized', message => $message, status => $status)
Branch: 1 branch
BranchTrueFalseTotal
if $status == 401 3 41 44
61 if $status == 401;
62 41 ▶ return $class->new(kind => 'forbidden', message => $message, status => $status)
Branch: 1 branch
BranchTrueFalseTotal
if $status == 403 8 33 41
63 if $status == 403;
64 # Any other 4xx is a CLIENT error. Falling through to the server_error
65 # default would make it retryable, so a bad dataset ID would be retried
66 # twice before failing. Only 5xx and transport failures are worth a retry.
67 33 ▶ return $class->new(kind => 'bad_request', message => $message, status => $status)
Branch: 1 branch
BranchTrueFalseTotal
if $status < 500 8 25 33
68 if $status < 500;
69 25 return $class->new(kind => 'server_error', message => $message, status => $status);
70 }
71  
72 # A per-entry failure inside a successful batch: the status the single lookup
73 # would have answered, and its message, with no headers at all - so a 429 here
74 # is a spent allowance, which is the only kind the API puts in an entry.
75 3 ▶sub from_entry {
Subroutine from_entry: called CC=1 SCAR=0.0
Pod undocumented
76 3 my ($class, $status, $message) = @_;
77 3 return $class->from_response($status, Mojo::Headers->new, { error => $message });
78 }
79  
80 # Anything a promise can reject with becomes one of these. Mojo::UserAgent
81 # rejects with a plain string when a connection never got far enough to have a
82 # status, which is exactly the transport failure the retry rule wants.
83 680 ▶sub wrap {
Subroutine wrap: called CC=4 SCAR=21.7
Pod undocumented
84 680 my ($class, $err) = @_;
85 680 ▶ return $err if Scalar::Util::blessed($err) && $err->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($err) and $err->isa(__PACKAGE__) 680 0 680
Condition33%1/3
builtin::blessed($err) and $err->isa(__PACKAGE__)
!ll&&!rl&&r
00680
MC/DC0%0/2
builtin::blessed($err) and $err->isa(__PACKAGE__)
builtin::blessed($err) $err->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa(__PACKAGE__)
ABresult
0X0
100
111
86 0 ▶ my $message = defined $err ? "$err" : 'request failed';
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 0 0 0
87 0 $message =~ s/\s+\z//;
88 0 return $class->new(kind => 'network', message => $message);
89 }
90  
91 630 ▶sub kind { $_[0]->{kind} }
Subroutine kind: called CC=1 SCAR=0.0
Pod documented
92 25 ▶sub message { $_[0]->{message} }
Subroutine message: called CC=1 SCAR=0.0
Pod documented
93 532 ▶sub status { $_[0]->{status} }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
94 97 ▶sub retry_after { $_[0]->{retry_after} }
Subroutine retry_after: called CC=1 SCAR=0.0
Pod documented
95  
96 165 ▶sub retryable {
Subroutine retryable: called CC=2 SCAR=6.9
Pod documented
97 165 ▶ return $RETRYABLE{ $_[0]->{kind} } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$RETRYABLE{$_[0]{'kind'}} ? : 141 24 165
98 }
99  
100 # An empty field says nothing, so it gives way to the other envelope, then to
101 # the status, rather than reading as a message of nothing at all (3.8.0 answered
102 # `{"error": "", "rc": "NOT_LICENSED"}` with an empty message).
103 91 ▶sub _message {
Subroutine _message: called CC=6 SCAR=17.9
104 91 my ($body) = @_;
105 91 ▶ return undef unless ref $body eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 11 80 91
106 80 for my $key (qw(error rc)) {
107 89 ▶ return $body->{$key} if defined $body->{$key} && !ref $body->{$key} && length $body->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $body->{$key} and not ref $body->{$key} and length $body->{$key} 78 11 89
Condition83%5/6
defined $body->{$key} and not ref $body->{$key} and length $body->{$key}
!ll&&!rl&&r
8378
defined $body->{$key} and not ref $body->{$key}
!ll&&!rl&&r
8081
MC/DC66%2/3
defined $body->{$key} and not ref $body->{$key} and length $body->{$key}
defined $body->{$key} not ref $body->{$key} length $body->{$key}
Truth table75%3/4
A and not(B) and C
A= defined $body->{$key}B= not ref $body->{$key}C= length $body->{$key}
ABCresult
0XX0
10X0
1100
1111
108 }
109 2 return undef;
110 }
111  
112 # Seconds as digits, or an HTTP date, and nothing else, so a value that is
113 # neither is no Retry-After at all and the 429 a spent quota. RFC 9110 allows
114 # exactly three date forms, all in GMT. Through 3.8.0 a fraction was read as
115 # seconds, and Mojo::Date read ISO 8601, any zone as GMT and a day or month it
116 # does not know as Monday or January, each of which held a call on a date the
117 # server never sent.
118 91 ▶sub _retry_after {
Subroutine _retry_after: called CC=5 SCAR=16.1
119 91 my ($value) = @_;
120 91 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 75 16 91
121 16 $value =~ s/^\s+|\s+\z//g;
122 16 ▶ return $value + 0 if $value =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A[0-9]+\z/ 6 10 16
123  
124 10 my $epoch = _http_date($value);
125 10 ▶ return undef unless defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $epoch 7 3 10
126 3 my $seconds = $epoch - time;
127 3 ▶ return $seconds > 0 ? $seconds : 0;
Branch: 1 branch
BranchTrueFalseTotal
$seconds > 0 ? : 3 0 3
128 }
129  
130 # IMF-fixdate (Sun, 06 Nov 1994 08:49:37 GMT), RFC 850 (Sunday, 06-Nov-94
131 # 08:49:37 GMT) and asctime (Sun Nov 6 08:49:37 1994), as epoch seconds.
132 10 ▶sub _http_date {
Subroutine _http_date: called CC=6 SCAR=18.0
133 10 my ($value) = @_;
134 10 my ($day, $month, $year, $hour, $minute, $second);
135 10 ▶ if ($value =~ /\A(?:$DAY), ([0-9]{2}) ($MON) ([0-9]{4}) $TIME GMT\z/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value =~ /\A(?:$DAY), ([0-9]{2}) ($MON) ([0-9]{4}) $TIME GMT\z/) { } 2 8 10
elsif ($value =~ /\A(?:$WEEKDAY), ([0-9]{2})-($MON)-([0-9]{2}) $TIME GMT\z/) { } 1 7 8
elsif ($value =~ /\A(?:$DAY) ($MON) ( [1-9]|[0-9]{2}) $TIME ([0-9]{4})\z/) { } 1 6 7
136 2 ($day, $month, $year, $hour, $minute, $second) = ($1, $2, $3, $4, $5, $6);
137 }
138 elsif ($value =~ /\A(?:$WEEKDAY), ([0-9]{2})-($MON)-([0-9]{2}) $TIME GMT\z/) {
139 1 ($day, $month, $year, $hour, $minute, $second) = ($1, $2, $3, $4, $5, $6);
140 # A two-digit year more than 50 years ahead is the most recent past one
141 # with those digits, as RFC 9110 has a recipient read it.
142 1 my $now = (gmtime)[5] + 1900;
143 1 $year += $now - $now % 100;
144 1 ▶ $year -= 100 if $year > $now + 50;
Branch: 1 branch
BranchTrueFalseTotal
if $year > $now + 50 0 1 1
145 }
146 elsif ($value =~ /\A(?:$DAY) ($MON) ( [1-9]|[0-9]{2}) $TIME ([0-9]{4})\z/) {
147 1 ($month, $day, $hour, $minute, $second, $year) = ($1, $2, $3, $4, $5, $6);
148 }
149 else {
150 6 return undef;
151 }
152 # Already past, and Time::Local reads a year below 1000 as one after 1900.
153 4 ▶ return 0 if $year < 1970;
Branch: 1 branch
BranchTrueFalseTotal
if $year < 1970 0 4 4
154 # Time::Local refuses a day, hour or minute out of range, which is no date.
155 4 my $epoch = eval { Time::Local::timegm($second, $minute, $hour, $day + 0, $MONTH{$month}, $year) };
156 4 return $epoch;
157 }
158  
159 7 1;
160  
161 __END__
« VPNDetection/Oauth.pm ↑ Summary VPNDetection/Result.pm »