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

1from __future__ import annotations 

2 

3from typing import TYPE_CHECKING, Any 

4 

5from .enums import ErrorCode 

6from .responses import ErrorResponse 

7 

8if TYPE_CHECKING: 

9 from .._types.jsonrpc.responses import ErrorPayload 

10 

11__all__ = ( 

12 "FlowError", 

13 "InternalError", 

14 "InvalidParams", 

15 "InvalidRequest", 

16 "JsonRPCException", 

17 "MethodNotFound", 

18 "ParserError", 

19) 

20 

21 

22class JsonRPCException(Exception): 

23 r"""This is a generic class representing a JsonRPCException 

24 

25 Attributes 

26 ---------- 

27 code: :class:`int` 

28 The JsonRPC Error Code 

29 

30 .. versionadded: 2.0.0 

31 message: :class:`str` 

32 The message sent with the error 

33 

34 .. versionadded: 2.0.0 

35 data: Optional[Any] 

36 Any data sent with the error 

37 

38 .. versionadded: 2.0.0 

39 """ 

40 

41 code: int 

42 

43 def __init__(self, message: str, data: Any | None = None) -> None: 

44 self.message = message 

45 self.data = data 

46 

47 def to_response(self) -> ErrorResponse: 

48 return ErrorResponse(self.code, self.message, self.data) 

49 

50 

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. 

53 

54 .. versionadded: 2.0.0 

55 

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 """ 

65 

66 code = ErrorCode.parser_error.value 

67 

68 

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. 

71 

72 .. versionadded: 2.0.0 

73 

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 """ 

83 

84 code = ErrorCode.invalid_request.value 

85 

86 

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. 

89 

90 .. versionadded: 2.0.0 

91 

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 """ 

101 

102 code = ErrorCode.method_not_found.value 

103 

104 

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. 

107 

108 .. versionadded: 2.0.0 

109 

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 """ 

119 

120 code = ErrorCode.invalid_params.value 

121 

122 

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. 

125 

126 .. versionadded: 2.0.0 

127 

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 """ 

137 

138 code = ErrorCode.internal_error.value 

139 

140 

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. 

143 

144 .. versionadded: 2.0.0 

145 

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 """ 

155 

156 code = ErrorCode.server_error_start.value 

157 

158 

159def get_exception_from_json(data: ErrorPayload) -> JsonRPCException: 

160 code = data["code"] 

161 kwargs: dict[str, Any] = {"message": data["message"], "data": data.get("data")} 

162 

163 for cls in ( 

164 ParserError, 

165 InvalidParams, 

166 InvalidRequest, 

167 MethodNotFound, 

168 InternalError, 

169 ): 

170 if code == cls.code: 

171 return cls(**kwargs) 

172 

173 if ErrorCode.server_error_end.value <= code <= ErrorCode.server_error_start.value: 

174 error = FlowError(**kwargs) 

175 else: 

176 error = JsonRPCException(**kwargs) 

177 

178 error.code = code 

179 return error