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