Skip to content

Exceptions

Lightkube uses httpx for handling http requests and responses. Because of that, connectivity or timeout issues may raise exceptions. You can get familiar with the exceptions returned by httpx library here.

There are few lightkube specific exceptions:

lightkube.ConfigError

Bases: Exception

Configuration specific errors.

This exception is raised if a failure is encountered handling the kubernetes configuration:

from lightkube import Client, ConfigError

try:
    client = Client()
except ConfigError as e:
    print(e)

output:

Configuration file ~/.kube/config not found

lightkube.ApiError(request: httpx.Request = None, response: httpx.Response = None, status: Optional[dict] = None)

Bases: HTTPStatusError

Source code in src/lightkube/core/exceptions.py
37
38
39
def __init__(self, request: httpx.Request = None, response: httpx.Response = None, status: Optional[dict] = None) -> None:
    self.status = meta_v1.Status.from_dict(response.json() if response else status)
    super().__init__(self.status.message, request=request, response=response)

status: meta_v1.Status = meta_v1.Status.from_dict(response.json() if response else status) instance-attribute

This exception extends httpx.HTTPStatusError and is raised when an HTTP error is returned from kubernetes API. An extra status attribute is available with details about the failure using the standard model meta_v1.Status.

from lightkube import Client, ApiError

client = Client()
try:
    pod = client.get(Pod, name="not-existing-pod")
except ApiError as e:
    print(e.status)

output:

Status(
    apiVersion='v1', 
    code=404, 
    details=StatusDetails(
        causes=None, group=None, kind='pods', 
        name='not-existing-pod', retryAfterSeconds=None, uid=None
    ),
    kind='Status', 
    message='pods "not-existing-pod" not found', 
    metadata=ListMeta(
        continue_=None, remainingItemCount=None, resourceVersion=None, selfLink=None
    ),
    reason='NotFound',
    status='Failure'
)

lightkube.LoadResourceError

Bases: Exception

Error in loading a resource

This exception can be raised when loading an undefined resource using codecs.from_dict() or codecs.load_all_yaml() (See Load/Dump kubernetes objects).

lightkube.NotReadyError(name: str, message: str)

Bases: Exception

Some information is not ready yet.

Source code in src/lightkube/core/exceptions.py
25
26
27
28
def __init__(self, name: str, message: str) -> None:
    super().__init__()
    self.name = name
    self.message = message

message = message instance-attribute

name = name instance-attribute

__str__() -> str

Source code in src/lightkube/core/exceptions.py
30
31
def __str__(self) -> str:
    return f"{self.name} is not ready yet: {self.message}"

This exception is raised when attempting to access the list response attribute resourceVersion before the list has been consumed. For more details see List-Watch pattern

lightkube.exceptions.ObjectDeleted(name)

Bases: Exception

Object was unexpectedly deleted

Source code in src/lightkube/core/exceptions.py
53
54
def __init__(self, name):
    self.name = name

name = name instance-attribute

__str__()

Source code in src/lightkube/core/exceptions.py
56
57
def __str__(self):
    return f"{self.name} was unexpectedly deleted"

This exception is raised when waiting for an object condition using client.wait(...), but the object itself get deleted. This is to prevent an infinite wait.

lightkube.exceptions.ConditionError(name, messages)

Bases: Exception

Object is in specified bad condition

Source code in src/lightkube/core/exceptions.py
65
66
67
def __init__(self, name, messages):
    self.name = name
    self.messages = messages

messages = messages instance-attribute

name = name instance-attribute

__str__()

Source code in src/lightkube/core/exceptions.py
69
70
71
def __str__(self):
    messages = "; ".join(self.messages)
    return f"{self.name} has failure condition(s): {messages}"

This exception is raised when waiting for an object condition using client.wait(...), if the condition matches one of the conditions in raise_for_conditions parameter.