| 1 | package Kubernetes::REST::APIError; | ||||||||||||||||||||||||||||||||
| 2 | 63 | our $VERSION = '1.109'; | |||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: An HTTP error status answered by the Kubernetes API | ||||||||||||||||||||||||||||||||
| 4 | 63 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 63 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 63 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 63 | ▶ | use Types::Standard qw(Int Maybe Str); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 63 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | use overload | ||||||||||||||||||||||||||||||||
| 11 | 63 | ▶ | '""' => '_as_string', | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 17 | ▶ | bool => sub { 1 }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 63 | fallback => 1; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 63 | has code => (is => 'ro', isa => Int, required => 1); | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | has body => (is => 'ro', isa => Str, default => sub { '' }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | # Maybe: check_response may be called without a context, and the message | ||||||||||||||||||||||||||||||||
| 23 | # then reads "Kubernetes API error (): ...", as it always did. | ||||||||||||||||||||||||||||||||
| 24 | 63 | has context => (is => 'ro', isa => Maybe[Str]); | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 63 | has response => (is => 'ro'); | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | # The Kubernetes Status object the body holds, or undef when it holds none: | ||||||||||||||||||||||||||||||||
| 31 | # a proxy's HTML page, plain text, JSON of another shape, no body at all. | ||||||||||||||||||||||||||||||||
| 32 | 63 | has _status => (is => 'lazy'); | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 10 | ▶ | sub _build__status { | ||||||||||||||||||||||||||||||
|
Subroutine
_build__status: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 35 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 36 | # body is characters already, so the decoder must not expect bytes. | ||||||||||||||||||||||||||||||||
| 37 | 10 | my $status = eval { JSON::MaybeXS->new->decode($self->body) }; | |||||||||||||||||||||||||||||||
| 38 | 10 | ▶ | return unless ref $status eq 'HASH' && ($status->{kind} // '') eq 'Status'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
ref $status eq "HASH" and ($status->{"kind"} // "") eq "Status"
$status->{"kind"} // ""
MC/DC100%2/2
ref $status eq "HASH" and ($status->{"kind"} // "") eq "Status"
ref $status eq "HASH"
$status->{"kind"}
Truth table100%3/3
A and B
A= ref $status eq "HASH"B= $status->{"kind"}
| |||||||||||||||||||||||||||||||||
| 39 | 6 | return $status; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 63 | has reason => (is => 'lazy'); | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 9 | ▶ | sub _build_reason { ($_[0]->_status // {})->{reason} } | ||||||||||||||||||||||||||||||
|
Subroutine
_build_reason: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->_status // {}
MC/DC100%1/1
$_[0]->_status // {}
$_[0]->_status
Truth table100%2/2
A
A= $_[0]->_status
| |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 63 | has message => (is => 'lazy'); | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 7 | ▶ | sub _build_message { ($_[0]->_status // {})->{message} } | ||||||||||||||||||||||||||||||
|
Subroutine
_build_message: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->_status // {}
MC/DC100%1/1
$_[0]->_status // {}
$_[0]->_status
Truth table100%2/2
A
A= $_[0]->_status
| |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 63 | has details => (is => 'lazy'); | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 4 | ▶ | sub _build_details { ($_[0]->_status // {})->{details} } | ||||||||||||||||||||||||||||||
|
Subroutine
_build_details: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->_status // {}
MC/DC100%1/1
$_[0]->_status // {}
$_[0]->_status
Truth table100%2/2
A
A= $_[0]->_status
| |||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | # ' at FILE line N.' plus newline, as croak appends it; set by throw. | ||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | has _location => (is => 'ro', default => sub { '' }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 6 | ▶ | sub is_not_found { $_[0]->code == 404 } | ||||||||||||||||||||||||||||||
|
Subroutine
is_not_found: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 5 | ▶ | sub is_conflict { $_[0]->code == 409 } | ||||||||||||||||||||||||||||||
|
Subroutine
is_conflict: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 63 | our @CARP_NOT; | |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 72 | ▶ | sub throw { | ||||||||||||||||||||||||||||||
|
Subroutine
throw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 69 | 72 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 70 | # Trust the package that throws, so Carp skips its frames exactly as a | ||||||||||||||||||||||||||||||||
| 71 | # croak from there would: the location names the caller line the string | ||||||||||||||||||||||||||||||||
| 72 | # croak named before this was an object. | ||||||||||||||||||||||||||||||||
| 73 | 72 | local @CARP_NOT = (scalar caller); | |||||||||||||||||||||||||||||||
| 74 | 72 | die $class->new(%args, _location => Carp::shortmess('')); | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 76 | ▶ | sub _as_string { | ||||||||||||||||||||||||||||||
|
Subroutine
_as_string: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 79 | 76 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 80 | 76 | ▶ | return 'Kubernetes API error (' . ($self->context // '') . '): ' | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->context // ""
MC/DC100%1/1
$self->context // ""
$self->context
Truth table100%2/2
A
A= $self->context
| |||||||||||||||||||||||||||||||||
| 81 | . $self->code . ' ' . $self->body . $self->_location; | ||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 63 | 1; | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | =pod | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | Kubernetes::REST::APIError - An HTTP error status answered by the Kubernetes API | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | version 1.109 | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | my $pod = eval { $api->get('Pod', 'web', namespace => 'default') }; | ||||||||||||||||||||||||||||||||
| 103 | if (my $err = $@) { | ||||||||||||||||||||||||||||||||
| 104 | die $err unless ref $err && $err->isa('Kubernetes::REST::APIError'); | ||||||||||||||||||||||||||||||||
| 105 | if ($err->is_not_found) { | ||||||||||||||||||||||||||||||||
| 106 | # already gone | ||||||||||||||||||||||||||||||||
| 107 | } elsif ($err->is_conflict) { | ||||||||||||||||||||||||||||||||
| 108 | # changed in the meantime: re-read and retry | ||||||||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||||||
| 110 | warn $err->code . ' ' . ($err->reason // '') . ': ' | ||||||||||||||||||||||||||||||||
| 111 | . ($err->message // $err->body) . "\n"; | ||||||||||||||||||||||||||||||||
| 112 | die $err; | ||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | What L<Kubernetes::REST> dies with when the API server answers a request with | ||||||||||||||||||||||||||||||||
| 119 | an HTTP error status (400 and up) - from every method that checks a | ||||||||||||||||||||||||||||||||
| 120 | response, and from the public L<Kubernetes::REST/check_response> an async | ||||||||||||||||||||||||||||||||
| 121 | wrapper calls. | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | The object stringifies to exactly the message these errors carried as plain | ||||||||||||||||||||||||||||||||
| 124 | strings, location included: | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | Kubernetes API error (get Pod): 404 {"kind":"Status",...} at app.pl line 12. | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | so code that prints C<$@>, matches it against a regex or compares it with | ||||||||||||||||||||||||||||||||
| 129 | C<eq> keeps working. It is always true in boolean context. | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | Everything else - invalid arguments, a resource name that resolves to no | ||||||||||||||||||||||||||||||||
| 132 | class - still croaks with a plain string. Where such a message reports an | ||||||||||||||||||||||||||||||||
| 133 | API error behind it, such as a failed discovery read, it embeds this | ||||||||||||||||||||||||||||||||
| 134 | object's text. | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | Not to be confused with L<Kubernetes::REST::Error>, the exception class of | ||||||||||||||||||||||||||||||||
| 137 | the deprecated v0 API. | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | =head2 code | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | Required. The HTTP status code, as a number (C<404>, C<409>, C<500>, ...). | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | =head2 body | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | The response body, decoded from UTF-8 to characters - leniently, so a | ||||||||||||||||||||||||||||||||
| 146 | truncated or non-UTF-8 body still yields a string. Empty when there was none. | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | =head2 context | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | What was being done, as named in the message: C<get Pod>, | ||||||||||||||||||||||||||||||||
| 151 | C<delete IO::K8s::Api::Core::V1::Pod>, ... C<undef> when | ||||||||||||||||||||||||||||||||
| 152 | L<Kubernetes::REST/check_response> was given none. | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | =head2 response | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | The response object itself (a L<Kubernetes::REST::HTTPResponse>, or whatever | ||||||||||||||||||||||||||||||||
| 157 | the IO backend returned), for anything the other attributes do not carry. | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | =head2 reason | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | The C<reason> of the Kubernetes C<Status> object in the body - C<NotFound>, | ||||||||||||||||||||||||||||||||
| 162 | C<AlreadyExists>, C<Conflict>, C<Invalid>, C<Forbidden>, ... C<undef> when | ||||||||||||||||||||||||||||||||
| 163 | the body is not such an object. | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | =head2 message | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | The C<message> of the Kubernetes C<Status> object in the body, e.g. | ||||||||||||||||||||||||||||||||
| 168 | C<pods "web" not found>. C<undef> when the body is not such an object. | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | =head2 details | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | The C<details> of the Kubernetes C<Status> object in the body, as a hashref | ||||||||||||||||||||||||||||||||
| 173 | (C<name>, C<kind>, C<causes>, ...). C<undef> when the body is not such an | ||||||||||||||||||||||||||||||||
| 174 | object or carries none. | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | =head2 is_not_found | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | True for a C<404>: the resource does not exist (or no longer does). | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | =head2 is_conflict | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | True for a C<409>: the resource already exists, or changed since the | ||||||||||||||||||||||||||||||||
| 183 | C<resourceVersion> that was sent. | ||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | =head2 throw | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | Kubernetes::REST::APIError->throw( | ||||||||||||||||||||||||||||||||
| 188 | code => $response->status, | ||||||||||||||||||||||||||||||||
| 189 | body => $decoded_body, | ||||||||||||||||||||||||||||||||
| 190 | context => 'get Pod', | ||||||||||||||||||||||||||||||||
| 191 | response => $response, | ||||||||||||||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | Construct the error and die with it. The location it stringifies with is | ||||||||||||||||||||||||||||||||
| 195 | the one C<croak> would add at that point: the first caller outside the | ||||||||||||||||||||||||||||||||
| 196 | throwing package. | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | =over | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | =item * L<Kubernetes::REST> - Main API client | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | =item * L<Kubernetes::REST/check_response> - Where it is thrown | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | =item * L<https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/status/> - The Kubernetes Status object | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | =back | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | =head2 Issues | ||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||
| 215 | L<https://github.com/pplu/kubernetes-rest/issues>. | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | =head2 IRC | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | =over 4 | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | =item * | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | =item * | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | Jose Luis Martinez Torres <jlmartin@cpan.org> | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | =back | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>. | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | =cut | ||||||||||||||||||||||||||||||||