|
1
|
|
|
import sys |
|
2
|
|
|
import traceback |
|
3
|
|
|
import typing |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
__all__ = ( |
|
7
|
|
|
'JsonRpcError', |
|
8
|
|
|
'ServerError', |
|
9
|
|
|
'ParseError', |
|
10
|
|
|
'InvalidRequest', |
|
11
|
|
|
'MethodNotFound', |
|
12
|
|
|
'InvalidParams', |
|
13
|
|
|
'InternalError', |
|
14
|
|
|
'DEFAULT_KNOWN_ERRORS', |
|
15
|
|
|
) |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class JsonRpcError(RuntimeError): |
|
19
|
|
|
code: typing.Optional[int] = None |
|
20
|
|
|
message: typing.Optional[str] = None |
|
21
|
|
|
data: typing.Optional[typing.Any] = None |
|
22
|
|
|
|
|
23
|
|
|
def __init__(self, |
|
24
|
|
|
message: typing.Optional[str] = None, *, |
|
25
|
|
|
data: typing.Optional[typing.Any] = None, |
|
26
|
|
|
code: typing.Optional[typing.Any] = None) -> None: |
|
27
|
|
|
super().__init__(self) |
|
28
|
|
|
self.message = message or self.message |
|
29
|
|
|
self.data = data |
|
30
|
|
|
self.code = code or self.code |
|
31
|
|
|
assert self.code, 'Error without code is not allowed.' |
|
32
|
|
|
|
|
33
|
|
|
def __repr__(self) -> str: |
|
34
|
|
|
return f'JsonRpcError({self.code}): {self.message}' |
|
35
|
|
|
|
|
36
|
|
|
def __str__(self) -> str: |
|
37
|
|
|
return self.__repr__() |
|
38
|
|
|
|
|
39
|
|
|
def __eq__(self, other: typing.Any) -> bool: |
|
40
|
|
|
return ( |
|
41
|
|
|
isinstance(other, JsonRpcError) |
|
42
|
|
|
and self.code == other.code |
|
43
|
|
|
and self.message == other.message |
|
44
|
|
|
and self.data == other.data |
|
45
|
|
|
) |
|
46
|
|
|
|
|
47
|
|
|
def with_traceback(self, exc_info=None, traceback_exception=None) -> 'JsonRpcError': |
|
48
|
|
|
if not traceback_exception: |
|
49
|
|
|
traceback_exception = traceback.TracebackException(*sys.exc_info()) |
|
50
|
|
|
|
|
51
|
|
|
if self.data is None: |
|
52
|
|
|
self.data = {} |
|
53
|
|
|
|
|
54
|
|
|
if isinstance(self.data, dict): |
|
55
|
|
|
self.data['traceback_exception'] = ''.join(traceback_exception.format()).split("\n") |
|
56
|
|
|
|
|
57
|
|
|
return self |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
class ServerError(JsonRpcError): |
|
61
|
|
|
code = -32000 |
|
62
|
|
|
message = 'Server error.' |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
class ParseError(JsonRpcError): |
|
66
|
|
|
code = -32700 |
|
67
|
|
|
message = 'Invalid JSON was received by the server.' |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
class InvalidRequest(JsonRpcError): |
|
71
|
|
|
code = -32600 |
|
72
|
|
|
message = 'The JSON sent is not a valid Request object.' |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
class MethodNotFound(JsonRpcError): |
|
76
|
|
|
code = -32601 |
|
77
|
|
|
message = 'The method does not exist / is not available.' |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
class InvalidParams(JsonRpcError): |
|
81
|
|
|
code = -32602 |
|
82
|
|
|
message = 'Invalid method parameter(s).' |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
class InternalError(JsonRpcError): |
|
86
|
|
|
code = -32603 |
|
87
|
|
|
message = 'Internal JSON-RPC error.' |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
DEFAULT_KNOWN_ERRORS = frozenset({ |
|
91
|
|
|
ServerError, |
|
92
|
|
|
ParseError, |
|
93
|
|
|
InvalidRequest, |
|
94
|
|
|
MethodNotFound, |
|
95
|
|
|
InvalidParams, |
|
96
|
|
|
InternalError, |
|
97
|
|
|
}) |
|
98
|
|
|
|