File: exceptions.py

package info (click to toggle)
python-go2rtc-client 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 788 kB
  • sloc: python: 1,127; makefile: 3
file content (68 lines) | stat: -rw-r--r-- 1,813 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
"""Go2rtc client exceptions."""

from __future__ import annotations

from functools import wraps
from typing import TYPE_CHECKING, Any

from aiohttp import ClientError
from mashumaro.exceptions import (
    ExtraKeysError,
    InvalidFieldValue,
    MissingDiscriminatorError,
    MissingField,
    SuitableVariantNotFoundError,
    UnserializableDataError,
)

if TYPE_CHECKING:
    from collections.abc import Callable, Coroutine


class Go2RtcClientError(Exception):
    """Base exception for go2rtc client."""


class Go2RtcVersionError(Exception):
    """Base exception for go2rtc client."""

    def __init__(
        self,
        server_version: str | None,
        min_version_supported: str,
        max_version_supported: str,
    ) -> None:
        """Initialize."""
        self._server_version = server_version
        self._min_version_supported = min_version_supported
        self._max_version_supported = max_version_supported

    def __str__(self) -> str:
        """Return exception message."""
        return (
            f"server version '{self._server_version}' not "
            f">= {self._min_version_supported} and < {self._max_version_supported}"
        )


def handle_error[**P, R](
    func: Callable[P, Coroutine[Any, Any, R]],
) -> Callable[P, Coroutine[Any, Any, R]]:
    """Wrap aiohttp and mashumaro errors."""

    @wraps(func)
    async def _func(*args: P.args, **kwargs: P.kwargs) -> R:
        try:
            return await func(*args, **kwargs)
        except (
            ClientError,
            ExtraKeysError,
            InvalidFieldValue,
            MissingDiscriminatorError,
            MissingField,
            SuitableVariantNotFoundError,
            UnserializableDataError,
        ) as exc:
            raise Go2RtcClientError from exc

    return _func