File: errors.py

package info (click to toggle)
simplisafe-python 2024.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,268 kB
  • sloc: python: 5,252; sh: 50; makefile: 19
file content (101 lines) | stat: -rw-r--r-- 2,047 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
94
95
96
97
98
99
100
101
"""Define package errors."""
from __future__ import annotations

from typing import Any


class SimplipyError(Exception):
    """A base error."""

    pass


class EndpointUnavailableError(SimplipyError):
    """An error related to accessing an endpoint that isn't available in the plan."""

    pass


class InvalidCredentialsError(SimplipyError):
    """An error related to invalid credentials."""

    pass


class MaxUserPinsExceededError(SimplipyError):
    """An error related to exceeding the maximum number of user PINs."""

    pass


class PinError(SimplipyError):
    """An error related to invalid PINs or PIN operations."""

    pass


class RequestError(SimplipyError):
    """An error related to invalid requests."""

    pass


class WebsocketError(SimplipyError):
    """An error related to generic websocket errors."""

    pass


class CannotConnectError(WebsocketError):
    """Define a error when the websocket can't be connected to."""

    pass


class ConnectionClosedError(WebsocketError):
    """Define a error when the websocket closes unexpectedly."""

    pass


class ConnectionFailedError(WebsocketError):
    """Define a error when the websocket connection fails."""

    pass


class InvalidMessageError(WebsocketError):
    """Define a error related to an invalid message from the websocket server."""

    pass


class NotConnectedError(WebsocketError):
    """Define a error when the websocket isn't properly connected to."""

    pass


DATA_ERROR_MAP: dict[str, type[SimplipyError]] = {
    "NoRemoteManagement": EndpointUnavailableError,
    "PinError": PinError,
}


def raise_on_data_error(data: dict[str, Any] | None) -> None:
    """Raise a specific error if the data payload suggests there is one.

    Args:
        data: An optional API response payload.

    Raises:
        error: A SimplipyError subclass.
    """
    if not data:
        return

    if (error_type := data.get("type")) not in DATA_ERROR_MAP:
        return

    error = DATA_ERROR_MAP[error_type](data["message"])
    raise error