Skip to content

Исключения

Эти классы исключений используются в реализациях клиента.

Базовые

Bases: ABC, Exception

Base class for all exceptions raised by Horizon.

Source code in horizon/commons/exceptions/base.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class ApplicationError(ABC, Exception):
    """Base class for all exceptions raised by Horizon."""

    @property
    @abstractmethod
    def message(self) -> str:
        """Message string"""
        ...

    @property
    @abstractmethod
    def details(self) -> Any:
        """Details related to specific error"""
        ...

    def __str__(self) -> str:
        return self.message

message abstractmethod property

Message string

details abstractmethod property

Details related to specific error

Авторизация

Bases: ABC, Exception

Base class for all exceptions raised by Horizon.

Source code in horizon/commons/exceptions/base.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class ApplicationError(ABC, Exception):
    """Base class for all exceptions raised by Horizon."""

    @property
    @abstractmethod
    def message(self) -> str:
        """Message string"""
        ...

    @property
    @abstractmethod
    def details(self) -> Any:
        """Details related to specific error"""
        ...

    def __str__(self) -> str:
        return self.message

message abstractmethod property

Message string

details abstractmethod property

Details related to specific error

Разрешения

Bases: ApplicationError

Permission denied for performing the requested action.

Examples

from horizon.commons.exceptions import PermissionDeniedError raise PermissionDeniedError(required_role="DEVELOPER", actual_role="GUEST") Traceback (most recent call last): horizon.commons.exceptions.PermissionDeniedError: Permission denied. User has role GUEST but action requires at least DEVELOPER.

Source code in horizon/commons/exceptions/permission.py
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
class PermissionDeniedError(ApplicationError):
    """
    Permission denied for performing the requested action.

    Examples
    --------

    >>> from horizon.commons.exceptions import PermissionDeniedError
    >>> raise PermissionDeniedError(required_role="DEVELOPER", actual_role="GUEST")
    Traceback (most recent call last):
    horizon.commons.exceptions.PermissionDeniedError: Permission denied. User has role GUEST but action requires at least DEVELOPER.
    """  # noqa: E501

    required_role: str
    """Required role to perform action"""

    actual_role: str
    """Actual user role"""

    def __init__(self, required_role: str, actual_role: str):
        self.required_role = required_role
        self.actual_role = actual_role

    @property
    def message(self) -> str:
        return f"Permission denied. User has role {self.actual_role} but action requires at least {self.required_role}."

    @property
    def details(self) -> dict[str, Any]:
        return {
            "required_role": self.required_role,
            "actual_role": self.actual_role,
        }

message property

details property

required_role = required_role instance-attribute

Required role to perform action

actual_role = actual_role instance-attribute

Actual user role

Bases: ApplicationError

Bad request error.

This exception should be raised when a request cannot be processed due to client-side errors (e.g., invalid data, duplicate entries).

Examples

from horizon.commons.exceptions import BadRequestError raise BadRequestError("Duplicate username detected. Each username must appear only once.") Traceback (most recent call last): horizon.commons.exceptions.BadRequestError: Duplicate username detected. Each username must appear only once.

Source code in horizon/commons/exceptions/bad_request.py
 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
class BadRequestError(ApplicationError):
    """Bad request error.

    This exception should be raised when a request cannot be processed due to
    client-side errors (e.g., invalid data, duplicate entries).

    Examples
    --------

    >>> from horizon.commons.exceptions import BadRequestError
    >>> raise BadRequestError("Duplicate username detected. Each username must appear only once.")
    Traceback (most recent call last):
    horizon.commons.exceptions.BadRequestError: Duplicate username detected. Each username must appear only once.
    """

    reason: str
    """Bad request reason message"""

    def __init__(self, reason: str):
        self.reason = reason

    @property
    def message(self) -> str:
        return self.reason

    @property
    def details(self) -> dict:
        return {}

reason = reason instance-attribute

Bad request reason message

Сущность

Bases: ApplicationError

Entity not found.

Examples

from horizon.commons.exceptions import EntityNotFoundError raise EntityNotFoundError("User", "username", "test") Traceback (most recent call last): horizon.commons.exceptions.entity.EntityNotFoundError: User with username='test' not found

Source code in horizon/commons/exceptions/entity.py
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
class EntityNotFoundError(ApplicationError):
    """Entity not found.

    Examples
    --------

    >>> from horizon.commons.exceptions import EntityNotFoundError
    >>> raise EntityNotFoundError("User", "username", "test")
    Traceback (most recent call last):
    horizon.commons.exceptions.entity.EntityNotFoundError: User with username='test' not found
    """

    entity_type: str
    """Entity type"""

    field: str
    """Entity identifier field"""

    value: Any
    """Entity identifier value"""

    def __init__(self, entity_type: str, field: str, value: Any):
        self.entity_type = entity_type
        self.field = field
        self.value = value

    @property
    def message(self) -> str:
        if self.field is not None:
            return f"{self.entity_type} with {self.field}={self.value!r} not found"
        return f"{self.entity_type} not found"

    @property
    def details(self) -> dict[str, Any]:
        return {
            "entity_type": self.entity_type,
            "field": self.field,
            "value": self.value,
        }

message property

details property

entity_type = entity_type instance-attribute

Entity type

field = field instance-attribute

Entity identifier field

value = value instance-attribute

Entity identifier value

Bases: ApplicationError

Entity with same identifier already exists.

Examples

from horizon.commons.exceptions import EntityNotFoundError raise EntityAlreadyExistsError("User", "username", "test") Traceback (most recent call last): horizon.commons.exceptions.entity.EntityAlreadyExistsError: User with username='test' already exists

Source code in horizon/commons/exceptions/entity.py
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
class EntityAlreadyExistsError(ApplicationError):
    """Entity with same identifier already exists.

    Examples
    --------


    >>> from horizon.commons.exceptions import EntityNotFoundError
    >>> raise EntityAlreadyExistsError("User", "username", "test")
    Traceback (most recent call last):
    horizon.commons.exceptions.entity.EntityAlreadyExistsError: User with username='test' already exists
    """

    entity_type: str
    """Entity type"""

    field: str
    """Entity identifier field"""

    value: Any
    """Entity identifier value"""

    def __init__(self, entity_type: str, field: str, value: Any):
        self.entity_type = entity_type
        self.field = field
        self.value = value

    @property
    def message(self) -> str:
        return f"{self.entity_type} with {self.field}={self.value!r} already exists"

    @property
    def details(self) -> dict[str, Any]:
        return {
            "entity_type": self.entity_type,
            "field": self.field,
            "value": self.value,
        }

message property

details property

entity_type = entity_type instance-attribute

Entity type

field = field instance-attribute

Entity identifier field

value = value instance-attribute

Entity identifier value

Сервис

Bases: ApplicationError

Service used by application have not responded properly.

Examples

from horizon.commons.exceptions import ServiceError raise ServiceError("Some server response is invalid") Traceback (most recent call last): horizon.commons.exceptions.service.ServiceError: Some server response is invalid

Source code in horizon/commons/exceptions/service.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class ServiceError(ApplicationError):
    """Service used by application have not responded properly.

    Examples
    --------

    >>> from horizon.commons.exceptions import ServiceError
    >>> raise ServiceError("Some server response is invalid")
    Traceback (most recent call last):
    horizon.commons.exceptions.service.ServiceError: Some server response is invalid
    """

    def __init__(self, message: str) -> None:
        self._message = message

    @property
    def message(self) -> str:
        return self._message

    @property
    def details(self) -> None:
        pass

message property