Coverage for flogin/jsonrpc/errors.py: 100%
35 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-03 22:51 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-03 22:51 +0000
1from __future__ import annotations
3from typing import TYPE_CHECKING, Any
5from .enums import ErrorCode
6from .responses import ErrorResponse
8if TYPE_CHECKING:
9 from .._types.jsonrpc.responses import ErrorPayload
11__all__ = (
12 "FlowError",
13 "InternalError",
14 "InvalidParams",
15 "InvalidRequest",
16 "JsonRPCException",
17 "MethodNotFound",
18 "ParserError",
19)
22class JsonRPCException(Exception):
23 r"""This is a generic class representing a JsonRPCException
25 Attributes
26 ----------
27 code: :class:`int`
28 The JsonRPC Error Code
30 .. versionadded: 2.0.0
31 message: :class:`str`
32 The message sent with the error
34 .. versionadded: 2.0.0
35 data: Optional[Any]
36 Any data sent with the error
38 .. versionadded: 2.0.0
39 """
41 code: int
43 def __init__(self, message: str, data: Any | None = None) -> None:
44 self.message = message
45 self.data = data
47 def to_response(self) -> ErrorResponse:
48 return ErrorResponse(self.code, self.message, self.data)
51class ParserError(JsonRPCException):
52 r"""This is a specialized JsonRPCException specifically for the error code -32700, which should mean that flogin sent invalid data and flow was unable to parse it.
54 .. versionadded: 2.0.0
56 Attributes
57 ----------
58 code: :class:`int` = -32700
59 The JsonRPC Error Code
60 message: :class:`str`
61 The message sent with the error
62 data: Optional[Any]
63 Any data sent with the error
64 """
66 code = ErrorCode.parser_error.value
69class InvalidRequest(JsonRPCException):
70 r"""This is a specialized JsonRPCException specifically for the error code -32600, which should mean that flogin sent an invalid request object.
72 .. versionadded: 2.0.0
74 Attributes
75 ----------
76 code: :class:`int` = -32600
77 The JsonRPC Error Code
78 message: :class:`str`
79 The message sent with the error
80 data: Optional[Any]
81 Any data sent with the error
82 """
84 code = ErrorCode.invalid_request.value
87class MethodNotFound(JsonRPCException):
88 r"""This is a specialized JsonRPCException specifically for the error code -32601, which should mean that flogin is attempting to use a method that doesn't exist.
90 .. versionadded: 2.0.0
92 Attributes
93 ----------
94 code: :class:`int` = -32601
95 The JsonRPC Error Code
96 message: :class:`str`
97 The message sent with the error
98 data: Optional[Any]
99 Any data sent with the error
100 """
102 code = ErrorCode.method_not_found.value
105class InvalidParams(JsonRPCException):
106 r"""This is a specialized JsonRPCException specifically for the error code -32602, which should mean that flogin is attempting to use a method, but is sending the wrong parameters.
108 .. versionadded: 2.0.0
110 Attributes
111 ----------
112 code: :class:`int` = -32602
113 The JsonRPC Error Code
114 message: :class:`str`
115 The message sent with the error
116 data: Optional[Any]
117 Any data sent with the error
118 """
120 code = ErrorCode.invalid_params.value
123class InternalError(JsonRPCException):
124 r"""This is a specialized JsonRPCException specifically for the error code -32603, which should mean that flogin has received an error.
126 .. versionadded: 2.0.0
128 Attributes
129 ----------
130 code: :class:`int` = -32603
131 The JsonRPC Error Code
132 message: :class:`str`
133 The message sent with the error
134 data: Optional[Any]
135 Any data sent with the error
136 """
138 code = ErrorCode.internal_error.value
141class FlowError(JsonRPCException):
142 r"""This is a specialized JsonRPCException specifically for error codes between -32000 and -32099, which means that flow has ran into an error.
144 .. versionadded: 2.0.0
146 Attributes
147 ----------
148 code: :class:`int`
149 The JsonRPC Error Code
150 message: :class:`str`
151 The message sent with the error
152 data: Optional[Any]
153 Any data sent with the error
154 """
156 code = ErrorCode.server_error_start.value
159def get_exception_from_json(data: ErrorPayload) -> JsonRPCException:
160 code = data["code"]
161 kwargs: dict[str, Any] = {"message": data["message"], "data": data.get("data")}
163 for cls in (
164 ParserError,
165 InvalidParams,
166 InvalidRequest,
167 MethodNotFound,
168 InternalError,
169 ):
170 if code == cls.code:
171 return cls(**kwargs)
173 if ErrorCode.server_error_end.value <= code <= ErrorCode.server_error_start.value:
174 error = FlowError(**kwargs)
175 else:
176 error = JsonRPCException(**kwargs)
178 error.code = code
179 return error