Skip to content

exceptions

Contains exceptions used by luduvo_api.

BadRequest

Bases: HTTPException

HTTP exception raised for status code 400.

Source code in luduvo/utilities/exceptions.py
111
112
113
114
class BadRequest(HTTPException):
    """HTTP exception raised for status code 400."""

    pass

Forbidden

Bases: HTTPException

HTTP exception raised for status code 403.

Source code in luduvo/utilities/exceptions.py
121
122
123
124
class Forbidden(HTTPException):
    """HTTP exception raised for status code 403."""

    pass

HTTPException

Bases: LuduvoException

Exception that's raised when an HTTP request fails.

Attributes:

Name Type Description
response Response

The HTTP response object.

status int

The HTTP response status code.

errors List[ResponseError]

A list of Luduvo response errors.

Source code in luduvo/utilities/exceptions.py
 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
102
103
104
105
106
107
108
class HTTPException(LuduvoException):
    """
    Exception that's raised when an HTTP request fails.

    Attributes:
        response: The HTTP response object.
        status: The HTTP response status code.
        errors: A list of Luduvo response errors.
    """

    def __init__(self, response: Response, errors: Optional[list] = None):
        """
        Arguments:
            response: The raw response object.
            errors: A list of errors.
        """
        self.response: Response = response
        self.status: int = response.status_code
        self.errors: List[ResponseError]

        if errors:
            self.errors = [ResponseError(data=error_data) for error_data in errors]
        else:
            self.errors = []

        if self.errors:
            error_string = self._generate_string()
            super().__init__(
                f"{response.status_code} {response.reason_phrase}: {response.url}.\n\nErrors:\n{error_string}"
            )
        else:
            super().__init__(
                f"{response.status_code} {response.reason_phrase}: {response.url}"
            )

    def _generate_string(self) -> str:
        parsed_errors = []
        for error in self.errors:
            # Make each error into a parsed string
            parsed_error = f"\t{error.code}: {error.message}"
            error_messages = []

            error.user_facing_message and error_messages.append(
                f"User-facing message: {error.user_facing_message}"
            )
            error.field and error_messages.append(f"Field: {error.field}")
            error.retryable and error_messages.append(f"Retryable: {error.retryable}")

            if error_messages:
                error_message_string = "\n\t\t".join(error_messages)
                parsed_error += f"\n\t\t{error_message_string}"

            parsed_errors.append(parsed_error)

        # Turn the parsed errors into a joined string
        return "\n".join(parsed_errors)

__init__(response, errors=None)

Parameters:

Name Type Description Default
response Response

The raw response object.

required
errors Optional[list]

A list of errors.

None
Source code in luduvo/utilities/exceptions.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def __init__(self, response: Response, errors: Optional[list] = None):
    """
    Arguments:
        response: The raw response object.
        errors: A list of errors.
    """
    self.response: Response = response
    self.status: int = response.status_code
    self.errors: List[ResponseError]

    if errors:
        self.errors = [ResponseError(data=error_data) for error_data in errors]
    else:
        self.errors = []

    if self.errors:
        error_string = self._generate_string()
        super().__init__(
            f"{response.status_code} {response.reason_phrase}: {response.url}.\n\nErrors:\n{error_string}"
        )
    else:
        super().__init__(
            f"{response.status_code} {response.reason_phrase}: {response.url}"
        )

InternalServerError

Bases: HTTPException

HTTP exception raised for status code 500.

Source code in luduvo/utilities/exceptions.py
145
146
147
148
149
150
class InternalServerError(HTTPException):
    """
    HTTP exception raised for status code 500.
    """

    pass

ItemNotFound

Bases: LuduvoException

Raised for invalid items.

Source code in luduvo/utilities/exceptions.py
177
178
179
180
181
182
183
184
185
186
187
188
189
class ItemNotFound(LuduvoException):
    """
    Raised for invalid items.
    """

    def __init__(self, message: str, response: Optional[Response] = None):
        """
        Arguments:
            response: The raw response object.
        """
        self.response: Optional[Response] = response
        self.status: Optional[int] = response.status_code if response else None
        super().__init__(message)

__init__(message, response=None)

Parameters:

Name Type Description Default
response Optional[Response]

The raw response object.

None
Source code in luduvo/utilities/exceptions.py
182
183
184
185
186
187
188
189
def __init__(self, message: str, response: Optional[Response] = None):
    """
    Arguments:
        response: The raw response object.
    """
    self.response: Optional[Response] = response
    self.status: Optional[int] = response.status_code if response else None
    super().__init__(message)

LuduvoException

Bases: Exception

Base exception for all of luduvo_api.

Source code in luduvo/utilities/exceptions.py
18
19
20
21
22
23
class LuduvoException(Exception):
    """
    Base exception for all of luduvo_api.
    """

    pass

NotFound

Bases: HTTPException

HTTP exception raised for status code 404. This usually means we have an internal URL issue - please make a GitHub issue about this!

Source code in luduvo/utilities/exceptions.py
127
128
129
130
131
132
133
class NotFound(HTTPException):
    """
    HTTP exception raised for status code 404.
    This usually means we have an internal URL issue - please make a GitHub issue about this!
    """

    pass

ResponseError

Represents an error returned by a Luduvo server.

Attributes:

Name Type Description
code int

The error code.

message Optional[str]

The error message.

user_facing_message Optional[str]

A more simple error message intended for frontend use.

field Optional[str]

The field causing this error.

retryable Optional[str]

Whether retrying this exception could suppress this issue.

Source code in luduvo/utilities/exceptions.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class ResponseError:
    """
    Represents an error returned by a Luduvo server.

    Attributes:
        code: The error code.
        message: The error message.
        user_facing_message: A more simple error message intended for frontend use.
        field: The field causing this error.
        retryable: Whether retrying this exception could suppress this issue.
    """

    def __init__(self, data: dict):
        self.code: int = data["code"]
        self.message: Optional[str] = data.get("message")
        self.user_facing_message: Optional[str] = data.get("userFacingMessage")
        self.field: Optional[str] = data.get("field")
        self.retryable: Optional[str] = data.get("retryable")

TooManyRequests

Bases: HTTPException

HTTP exception raised for status code 429. This means that Luduvo has ratelimited you.

Source code in luduvo/utilities/exceptions.py
136
137
138
139
140
141
142
class TooManyRequests(HTTPException):
    """
    HTTP exception raised for status code 429.
    This means that Luduvo has [ratelimited](https://en.wikipedia.org/wiki/Rate_limiting) you.
    """

    pass

Unauthorized

Bases: HTTPException

HTTP exception raised for status code 401. This usually means you aren't properly authenticated.

Source code in luduvo/utilities/exceptions.py
117
118
class Unauthorized(HTTPException):
    """HTTP exception raised for status code 401. This usually means you aren't properly authenticated."""

UserNotFound

Bases: ItemNotFound

Raised for invalid user IDs or usernames.

Source code in luduvo/utilities/exceptions.py
192
193
194
195
196
197
class UserNotFound(ItemNotFound):
    """
    Raised for invalid user IDs or usernames.
    """

    pass

get_exception_from_status_code(code)

Gets an exception that should be raised instead of the generic HTTPException for this status code.

Source code in luduvo/utilities/exceptions.py
163
164
165
166
167
168
169
170
171
172
173
def get_exception_from_status_code(code: int) -> Type[HTTPException]:
    """
    Gets an exception that should be raised instead of the generic HTTPException for this status code.
    """
    exception = _codes_exceptions.get(code) or HTTPException
    logger.debug(
        "Mapped HTTP status code %s to exception %s",
        code,
        exception.__name__,
    )
    return exception