19
19
from jsonschema .validators import validator_for # type: ignore
20
20
from pkg_resources import resource_string # type: ignore
21
21
22
+ from .exceptions import JsonRpcError
22
23
from .methods import Methods , global_methods , validate_args
23
24
from .request import Request , NOID
24
25
from .response import (
25
26
InvalidRequestResponse ,
26
- MethodNotFoundResponse ,
27
27
ParseErrorResponse ,
28
28
Response ,
29
29
ServerErrorResponse ,
30
30
from_result ,
31
31
to_serializable ,
32
32
)
33
- from .result import InvalidParams , InternalError , Error , Result , Success
33
+ from .result import InvalidParams , InternalError , MethodNotFound , Error , Result , Success
34
34
35
35
default_deserializer = json .loads
36
36
46
46
config .read ([".jsonrpcserverrc" , os .path .expanduser ("~/.jsonrpcserverrc" )])
47
47
48
48
49
- def call (method : Callable , args : list , kwargs : dict ) -> Result :
49
+ def call (methods : Methods , method_name : str , args : list , kwargs : dict ) -> Result :
50
50
"""
51
51
Calls a method.
52
52
@@ -55,20 +55,28 @@ def call(method: Callable, args: list, kwargs: dict) -> Result:
55
55
Returns:
56
56
The Result from the method call.
57
57
"""
58
+ try :
59
+ method = methods .items [method_name ]
60
+ except KeyError :
61
+ return MethodNotFound (method_name )
62
+
58
63
errors = validate_args (method , * args , ** kwargs )
59
64
if errors :
60
65
return InvalidParams (errors )
61
66
62
67
try :
63
68
result = method (* args , ** kwargs )
69
+ except JsonRpcError as exc :
70
+ return Error (code = exc .code , message = exc .message , data = exc .data )
71
+ except Exception as exc : # Other error inside method - server error
72
+ logging .exception (exc )
73
+ return InternalError (str (exc ))
74
+ else :
64
75
return (
65
76
InternalError ("The method did not return a Result" )
66
77
if not isinstance (result , (Success , Error ))
67
78
else result
68
79
)
69
- except Exception as exc : # Other error inside method - server error
70
- logging .exception (exc )
71
- return InternalError (str (exc ))
72
80
73
81
74
82
def extract_args (request : Request , context : Any ) -> list :
@@ -90,15 +98,13 @@ def dispatch_request(
90
98
91
99
Converts the return value (a Result) into a Response.
92
100
"""
93
- if request .method in methods .items :
94
- result = call (
95
- methods .items [request .method ],
96
- extract_args (request , context ),
97
- extract_kwargs (request ),
98
- )
99
- return None if request .id is NOID else from_result (result , request .id )
100
- else :
101
- return MethodNotFoundResponse (request .method , request .id )
101
+ result = call (
102
+ methods ,
103
+ request .method ,
104
+ extract_args (request , context ),
105
+ extract_kwargs (request ),
106
+ )
107
+ return None if request .id is NOID else from_result (result , request .id )
102
108
103
109
104
110
def none_if_empty (x : Any ) -> Any :
@@ -169,8 +175,8 @@ def dispatch_to_response_pure(
169
175
testing, not dispatch_to_response or dispatch.
170
176
171
177
Args:
172
- deserializer: Function that is used to deserialize data .
173
- schema_validator:
178
+ deserializer: Function that deserializes the JSON-RPC request .
179
+ schema_validator: Function that validates the JSON-RPC request.
174
180
context: Will be passed to methods as the first param if not None.
175
181
methods: Collection of methods that can be called.
176
182
request: The incoming request string.
@@ -223,8 +229,8 @@ def dispatch_to_response(
223
229
internal, global methods object which is populated with the @method
224
230
decorator.
225
231
context: Will be passed to methods as the first param if not None.
226
- schema_validator:
227
- deserializer: Function that is used to deserialize data .
232
+ schema_validator: Function that validates the JSON-RPC request.
233
+ deserializer: Function that deserializes the JSON-RPC request .
228
234
229
235
Returns:
230
236
A Response, list of Responses or None.
0 commit comments