blib/lib/Kubernetes/REST/APIError.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 92 CC 2 SCAR 9.3
CC
2
Cov
0%
_build__status
6.0
_build_reason
2.0
_build_message
2.0
CRAP 2.5
1 package Kubernetes::REST::APIError;
2 our $VERSION = '1.109';
3 # ABSTRACT: An HTTP error status answered by the Kubernetes API
4 use Moo;
5 use Carp ();
6 use JSON::MaybeXS ();
7 use Types::Standard qw(Int Maybe Str);
8 use namespace::clean;
9  
10 use overload
11 '""' => '_as_string',
12 bool => sub { 1 },
13 fallback => 1;
14  
15  
16 has code => (is => 'ro', isa => Int, required => 1);
17  
18  
19 has body => (is => 'ro', isa => Str, default => sub { '' });
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 has context => (is => 'ro', isa => Maybe[Str]);
25  
26  
27 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 has _status => (is => 'lazy');
33  
34 sub _build__status {
35 my ($self) = @_;
36 # body is characters already, so the decoder must not expect bytes.
37 my $status = eval { JSON::MaybeXS->new->decode($self->body) };
38 return unless ref $status eq 'HASH' && ($status->{kind} // '') eq 'Status';
39 return $status;
40 }
41  
42 has reason => (is => 'lazy');
43  
44 sub _build_reason { ($_[0]->_status // {})->{reason} }
45  
46  
47 has message => (is => 'lazy');
48  
49 sub _build_message { ($_[0]->_status // {})->{message} }
50  
51  
52 has details => (is => 'lazy');
53  
54 sub _build_details { ($_[0]->_status // {})->{details} }
55  
56  
57 # ' at FILE line N.' plus newline, as croak appends it; set by throw.
58 has _location => (is => 'ro', default => sub { '' });
59  
60 sub is_not_found { $_[0]->code == 404 }
61  
62  
63 sub is_conflict { $_[0]->code == 409 }
64  
65  
66 our @CARP_NOT;
67  
68 sub throw {
69 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 local @CARP_NOT = (scalar caller);
74 die $class->new(%args, _location => Carp::shortmess(''));
75 }
76  
77  
78 sub _as_string {
79 my ($self) = @_;
80 return 'Kubernetes API error (' . ($self->context // '') . '): '
81 . $self->code . ' ' . $self->body . $self->_location;
82 }
83  
84 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