| 1 | package InternetData::Error; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Mojo::Headers; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | 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 | 101 | ▶ | 'bool' => sub { 1 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 8 | fallback => 1; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 8 | our $VERSION = '1.15.0'; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | my %RETRYABLE = (rate_limited => 1, server_error => 1, network => 1); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 8 | my %MONTH; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 8 | @MONTH{qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)} = 0 .. 11; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 8 | my $DAY = join '|', qw(Mon Tue Wed Thu Fri Sat Sun); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 8 | my $WEEKDAY = join '|', qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday); | |||||||||||||||||||||||||||||||||||||||||
| 23 | 8 | my $MON = join '|', keys %MONTH; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 8 | my $TIME = '([0-9]{2}):([0-9]{2}):([0-9]{2})'; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 983 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 983 | 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 | 983 | ▶ | }, $class; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # The two APIs behind this host answer with different envelopes - a lookup's | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # refusal says why under `error`, a database's under `rc` - and both are read | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # here so a caller never has to know which was hit. A database code is more use | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # to a caller than the status alone: NOT_LICENSED and LICENSE_EXPIRED are both | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # 403 and mean different things to do next. $fallback names where a refusal came | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # from when there is no envelope to read, which is the case for object storage | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # on the other end of a presigned link. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 239 | ▶ | sub from_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_response: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 239 | my ($class, $status, $headers, $body, $fallback) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 239 | my $message = _message($body); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 239 | ▶ | $message = $fallback if !defined $message && defined $fallback; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $message and defined $fallback
MC/DC100%2/2
not defined $message and defined $fallback
not defined $message
defined $fallback
Truth table100%3/3
not(A) and B
A= not defined $messageB= defined $fallback
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 239 | ▶ | $message = "request failed with status $status" unless defined $message; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 239 | my $retry_after = _retry_after($headers->header('Retry-After')); | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Present means a transient rate limit and retrying works; absent means an | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # allowance is spent. Nothing else in the response separates the two. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 239 | ▶ | if ($status == 429) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 62 | ▶ | return $class->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | kind => defined $retry_after ? 'rate_limited' : 'quota_exceeded', | ||||||||||||||||||||||||||||||||||||||||||
| 62 | message => $message, status => $status, retry_after => $retry_after, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 177 | ▶ | return $class->new(kind => 'bad_request', message => $message, status => $status) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | if $status == 400; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 133 | ▶ | return $class->new(kind => 'unauthorized', message => $message, status => $status) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | if $status == 401; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 121 | ▶ | return $class->new(kind => 'forbidden', message => $message, status => $status) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | if $status == 403; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # Any other 4xx is a CLIENT error, and 404 is one this API actually serves: | ||||||||||||||||||||||||||||||||||||||||||
| 72 | # a database ID it does not publish, or a build not mirrored yet. Falling | ||||||||||||||||||||||||||||||||||||||||||
| 73 | # through to the server_error default would make it retryable, so a | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # misspelled ID would be requested three times before failing. Classified on | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # the RANGE rather than an enumerated list, so a status added later cannot | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # land in the wrong half. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 84 | ▶ | return $class->new(kind => 'bad_request', message => $message, status => $status) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | if $status < 500; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 59 | return $class->new(kind => 'server_error', message => $message, status => $status); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # A per-entry failure inside a successful batch: the status the single lookup | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # would have answered, and its message, with no headers at all - so a 429 here | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # is a spent allowance, which is the only kind the API puts in an entry. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | sub from_entry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_entry: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | my ($class, $status, $message) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | return $class->from_response($status, Mojo::Headers->new, { error => $message }); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # Anything a promise can reject with becomes one of these. Mojo::UserAgent | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # rejects with a plain string when a connection never got far enough to have a | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # status, which is exactly the transport failure the retry rule wants. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 895 | ▶ | sub wrap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 895 | my ($class, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 895 | ▶ | return $err if Scalar::Util::blessed($err) && $err->isa(__PACKAGE__); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($err) and $err->isa(__PACKAGE__)
MC/DC50%1/2
builtin::blessed($err) and $err->isa(__PACKAGE__)
builtin::blessed($err)
$err->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($err)B= $err->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | ▶ | my $message = defined $err ? "$err" : 'request failed'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 6 | $message =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 6 | return $class->new(kind => 'network', message => $message); | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 582 | ▶ | sub kind { $_[0]->{kind} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 91 | ▶ | sub message { $_[0]->{message} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 433 | ▶ | sub status { $_[0]->{status} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 332 | ▶ | sub retry_after { $_[0]->{retry_after} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retry_after: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 496 | ▶ | sub retryable { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retryable: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 496 | ▶ | return $RETRYABLE{ $_[0]->{kind} } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # An empty field says nothing, so it gives way to the other envelope, then to | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # the fallback, rather than reading as a message of nothing at all. | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 239 | ▶ | sub _message { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_message: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 239 | my ($body) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 239 | ▶ | return undef unless ref $body eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 226 | for my $key (qw(error rc)) { | |||||||||||||||||||||||||||||||||||||||||
| 116 | 359 | ▶ | return $body->{$key} if defined $body->{$key} && !ref $body->{$key} && length $body->{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $body->{$key} and not ref $body->{$key} and length $body->{$key}
defined $body->{$key} and not ref $body->{$key}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 40 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # Seconds as digits, or an HTTP date, and nothing else, so a value that is | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # neither is no Retry-After at all and the 429 a spent quota. RFC 9110 allows | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # exactly three date forms, all in GMT; Mojo::Date also reads ISO 8601, any zone | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # name as GMT and a month it does not know as January, each of which would hold | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # a call on a date the server never sent. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 239 | ▶ | sub _retry_after { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_retry_after: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 239 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 239 | ▶ | return undef unless defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 50 | $value =~ s/^\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 50 | ▶ | return $value + 0 if $value =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 26 | my $epoch = _http_date($value); | |||||||||||||||||||||||||||||||||||||||||
| 133 | 26 | ▶ | return undef unless defined $epoch; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 5 | my $seconds = $epoch - time; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 5 | ▶ | return $seconds > 0 ? $seconds : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # IMF-fixdate (Sun, 06 Nov 1994 08:49:37 GMT), RFC 850 (Sunday, 06-Nov-94 | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # 08:49:37 GMT) and asctime (Sun Nov 6 08:49:37 1994), as epoch seconds. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 26 | ▶ | sub _http_date { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_http_date: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 26 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 26 | my ($day, $month, $year, $hour, $minute, $second); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 26 | ▶ | if ($value =~ /\A(?:$DAY), ([0-9]{2}) ($MON) ([0-9]{4}) $TIME GMT\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 7 | ($day, $month, $year, $hour, $minute, $second) = ($1, $2, $3, $4, $5, $6); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | elsif ($value =~ /\A(?:$WEEKDAY), ([0-9]{2})-($MON)-([0-9]{2}) $TIME GMT\z/) { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | ($day, $month, $year, $hour, $minute, $second) = ($1, $2, $3, $4, $5, $6); | |||||||||||||||||||||||||||||||||||||||||
| 148 | # A two-digit year more than 50 years ahead is the most recent past one | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # with those digits, as RFC 9110 has a recipient read it. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | my $now = (gmtime)[5] + 1900; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | $year += $now - $now % 100; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | ▶ | $year -= 100 if $year > $now + 50; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | elsif ($value =~ /\A(?:$DAY) ($MON) ( [1-9]|[0-9]{2}) $TIME ([0-9]{4})\z/) { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | ($month, $day, $hour, $minute, $second, $year) = ($1, $2, $3, $4, $5, $6); | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 17 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # Already past, and Time::Local reads a year below 1000 as one after 1900. | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 9 | ▶ | return 0 if $year < 1970; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | # Time::Local refuses a day, hour or minute out of range, which is no date. | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 9 | my $epoch = eval { Time::Local::timegm($second, $minute, $hour, $day + 0, $MONTH{$month}, $year) }; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 9 | return $epoch; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||||||||||||||||