NAME

Kubernetes::REST::APIError - An HTTP error status answered by the Kubernetes API

VERSION

version 1.109

SYNOPSIS

my $pod = eval { $api->get('Pod', 'web', namespace => 'default') };
if (my $err = $@) {
    die $err unless ref $err && $err->isa('Kubernetes::REST::APIError');
    if ($err->is_not_found) {
        # already gone
    } elsif ($err->is_conflict) {
        # changed in the meantime: re-read and retry
    } else {
        warn $err->code . ' ' . ($err->reason // '') . ': '
            . ($err->message // $err->body) . "\n";
        die $err;
    }
}

DESCRIPTION

What Kubernetes::REST dies with when the API server answers a request with an HTTP error status (400 and up) - from every method that checks a response, and from the public "check_response" in Kubernetes::REST an async wrapper calls.

The object stringifies to exactly the message these errors carried as plain strings, location included:

Kubernetes API error (get Pod): 404 {"kind":"Status",...} at app.pl line 12.

so code that prints $@, matches it against a regex or compares it with eq keeps working. It is always true in boolean context.

Everything else - invalid arguments, a resource name that resolves to no class - still croaks with a plain string. Where such a message reports an API error behind it, such as a failed discovery read, it embeds this object's text.

Not to be confused with Kubernetes::REST::Error, the exception class of the deprecated v0 API.

code

Required. The HTTP status code, as a number (404, 409, 500, ...).

body

The response body, decoded from UTF-8 to characters - leniently, so a truncated or non-UTF-8 body still yields a string. Empty when there was none.

context

What was being done, as named in the message: get Pod, delete IO::K8s::Api::Core::V1::Pod, ... undef when "check_response" in Kubernetes::REST was given none.

response

The response object itself (a Kubernetes::REST::HTTPResponse, or whatever the IO backend returned), for anything the other attributes do not carry.

reason

The reason of the Kubernetes Status object in the body - NotFound, AlreadyExists, Conflict, Invalid, Forbidden, ... undef when the body is not such an object.

message

The message of the Kubernetes Status object in the body, e.g. pods "web" not found. undef when the body is not such an object.

details

The details of the Kubernetes Status object in the body, as a hashref (name, kind, causes, ...). undef when the body is not such an object or carries none.

is_not_found

True for a 404: the resource does not exist (or no longer does).

is_conflict

True for a 409: the resource already exists, or changed since the resourceVersion that was sent.

throw

Kubernetes::REST::APIError->throw(
    code     => $response->status,
    body     => $decoded_body,
    context  => 'get Pod',
    response => $response,
);

Construct the error and die with it. The location it stringifies with is the one croak would add at that point: the first caller outside the throwing package.

SEE ALSO

SUPPORT

Issues

Please report bugs and feature requests on GitHub at https://github.com/pplu/kubernetes-rest/issues.

IRC

Join #kubernetes on irc.perl.org or message Getty directly.

CONTRIBUTING

Contributions are welcome! Please fork the repository and submit a pull request.

AUTHORS

  • Torsten Raudssus <getty@cpan.org>

  • Jose Luis Martinez Torres <jlmartin@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>.

This is free software, licensed under:

The Apache License, Version 2.0, January 2004