File: exceptions.py

package info (click to toggle)
python-aiohasupervisor 0.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 932 kB
  • sloc: python: 4,666; sh: 37; makefile: 3
file content (93 lines) | stat: -rw-r--r-- 2,597 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Exceptions from supervisor client."""

from abc import ABC
from collections.abc import Callable
from typing import Any


class SupervisorError(Exception):
    """Generic exception."""

    error_key: str | None = None

    def __init__(
        self,
        message: str | None = None,
        extra_fields: dict[str, Any] | None = None,
        job_id: str | None = None,
    ) -> None:
        """Initialize exception."""
        if message is not None:
            super().__init__(message)
        else:
            super().__init__()

        self.job_id = job_id
        self.extra_fields = extra_fields


ERROR_KEYS: dict[str, type[SupervisorError]] = {}


def error_key(
    key: str,
) -> Callable[[type[SupervisorError]], type[SupervisorError]]:
    """Store exception in keyed error map."""

    def wrap(cls: type[SupervisorError]) -> type[SupervisorError]:
        ERROR_KEYS[key] = cls
        cls.error_key = key
        return cls

    return wrap


class SupervisorConnectionError(SupervisorError, ConnectionError):
    """Unknown error connecting to supervisor."""


class SupervisorTimeoutError(SupervisorError, TimeoutError):
    """Timeout connecting to supervisor."""


class SupervisorBadRequestError(SupervisorError):
    """Invalid request made to supervisor."""


class SupervisorAuthenticationError(SupervisorError):
    """Invalid authentication sent to supervisor."""


class SupervisorForbiddenError(SupervisorError):
    """Client is not allowed to take the action requested."""


class SupervisorNotFoundError(SupervisorError):
    """Requested resource does not exist."""


class SupervisorServiceUnavailableError(SupervisorError):
    """Cannot complete request because a required service is unavailable."""


class SupervisorResponseError(SupervisorError):
    """Unusable response received from Supervisor with the wrong type or encoding."""


class AddonNotSupportedError(SupervisorError, ABC):
    """Addon is not supported on this system."""


@error_key("addon_not_supported_architecture_error")
class AddonNotSupportedArchitectureError(AddonNotSupportedError):
    """Addon is not supported on this system due to its architecture."""


@error_key("addon_not_supported_machine_type_error")
class AddonNotSupportedMachineTypeError(AddonNotSupportedError):
    """Addon is not supported on this system due to its machine type."""


@error_key("addon_not_supported_home_assistant_version_error")
class AddonNotSupportedHomeAssistantVersionError(AddonNotSupportedError):
    """Addon is not supported on this system due to its version of Home Assistant."""