Skip to content

Commit 47a2c21

Browse files
committed
Remove debug option
Closes #112
1 parent d255049 commit 47a2c21

File tree

7 files changed

+31
-127
lines changed

7 files changed

+31
-127
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
- dispatch's context param renamed to "extra". This value is included in the
88
context object passed to every method.
99
- Removed "convert camel case" option.
10+
- Removed the custom exceptions. From methods, return an ErrorResponse instead
11+
of raising an exception.
1012

1113
Refactoring/internal changes:
1214

doc/api.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ below):
116116

117117
Adds log handlers, to log all requests and responses to stderr.
118118

119-
**debug**
120-
121-
If True, more information is included in error responses, such as an exception
122-
message. *For ApiError, this value is ignored.* Default is *False*.
123-
124119
**trim_log_values**
125120

126121
Show abbreviated requests and responses in logs. Default is *False*.
@@ -133,7 +128,6 @@ placed in the current or home directory:
133128
```ini
134129
[general]
135130
basic_logging = yes
136-
debug = yes
137131
trim_log_values = yes
138132
```
139133

@@ -157,8 +151,8 @@ The dispatcher will give the appropriate response:
157151
'{"jsonrpc": "2.0", "error": {"code": -32602, "message": "Invalid parameters"}, "id": 1}'
158152
```
159153

160-
To send some other application-defined error response, raise an `ApiError` in
161-
a similar way.
154+
To send some other application-defined error response, raise an `ApiError` in a
155+
similar way.
162156

163157
```python
164158
from jsonrpcserver.exceptions import ApiError
@@ -169,9 +163,6 @@ def my_method():
169163
raise ApiError("Can't fulfill the request")
170164
```
171165

172-
To include the message passed when raising the exception, set `debug` to True.
173-
(See Configuration above.)
174-
175166
## Async
176167

177168
Asyncio is supported, allowing requests to be dispatched to coroutines.

jsonrpcserver/async_dispatcher.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ async def call(method: Method, *args: Any, **kwargs: Any) -> Any:
3636

3737

3838
async def safe_call(
39-
request: Request, methods: Methods, *, debug: bool, extra: Any, serialize: Callable
39+
request: Request, methods: Methods, *, extra: Any, serialize: Callable
4040
) -> Response:
41-
with handle_exceptions(request, debug) as handler:
41+
with handle_exceptions(request) as handler:
4242
if isinstance(request.params, list):
4343
result = await call(
4444
lookup(methods, request.method),
@@ -62,40 +62,34 @@ async def safe_call(
6262
async def call_requests(
6363
requests: Union[Request, Iterable[Request]],
6464
methods: Methods,
65-
debug: bool,
6665
extra: Any,
6766
serialize: Callable,
6867
) -> Response:
6968
if isinstance(requests, collections.abc.Iterable):
7069
responses = (
71-
safe_call(r, methods, debug=debug, extra=extra, serialize=serialize)
72-
for r in requests
70+
safe_call(r, methods, extra=extra, serialize=serialize) for r in requests
7371
)
7472
return BatchResponse(await asyncio.gather(*responses), serialize_func=serialize)
75-
return await safe_call(
76-
requests, methods, debug=debug, extra=extra, serialize=serialize
77-
)
73+
return await safe_call(requests, methods, extra=extra, serialize=serialize)
7874

7975

8076
async def dispatch_pure(
8177
request: str,
8278
methods: Methods,
8379
*,
84-
debug: bool,
8580
extra: Any,
8681
serialize: Callable,
8782
deserialize: Callable,
8883
) -> Response:
8984
try:
9085
deserialized = validate(deserialize(request), schema)
9186
except JSONDecodeError as exc:
92-
return InvalidJSONResponse(data=str(exc), debug=debug)
87+
return InvalidJSONResponse(data=str(exc))
9388
except ValidationError as exc:
94-
return InvalidJSONRPCResponse(data=None, debug=debug)
89+
return InvalidJSONRPCResponse(data=None)
9590
return await call_requests(
9691
create_requests(deserialized),
9792
methods,
98-
debug=debug,
9993
extra=extra,
10094
serialize=serialize,
10195
)
@@ -107,7 +101,6 @@ async def dispatch(
107101
methods: Optional[Methods] = None,
108102
*,
109103
basic_logging: bool = False,
110-
debug: bool = False,
111104
extra: Optional[Any] = None,
112105
trim_log_values: bool = False,
113106
serialize: Callable = default_serialize,
@@ -123,7 +116,6 @@ async def dispatch(
123116
response = await dispatch_pure(
124117
request,
125118
methods,
126-
debug=debug,
127119
extra=extra,
128120
serialize=serialize,
129121
deserialize=deserialize,

jsonrpcserver/dispatcher.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,24 +135,20 @@ def call(method: Method, *args: Any, **kwargs: Any) -> Any:
135135

136136

137137
@contextmanager
138-
def handle_exceptions(request: Request, debug: bool) -> Generator:
138+
def handle_exceptions(request: Request) -> Generator:
139139
handler = SimpleNamespace(response=None)
140140
try:
141141
yield handler
142142
except MethodNotFoundError:
143-
handler.response = MethodNotFoundResponse(
144-
id=request.id, data=request.method, debug=debug
145-
)
143+
handler.response = MethodNotFoundResponse(id=request.id, data=request.method)
146144
except (InvalidParamsError, AssertionError) as exc:
147145
# InvalidParamsError is raised by validate_args. AssertionError is raised inside
148146
# the methods, however it's better to raise InvalidParamsError inside methods.
149147
# AssertionError will be removed in the next major release.
150-
handler.response = InvalidParamsResponse(
151-
id=request.id, data=str(exc), debug=debug
152-
)
148+
handler.response = InvalidParamsResponse(id=request.id, data=str(exc))
153149
except ApiError as exc: # Method signals custom error
154150
handler.response = ApiErrorResponse(
155-
str(exc), code=exc.code, data=exc.data, id=request.id, debug=debug
151+
str(exc), code=exc.code, data=exc.data, id=request.id
156152
)
157153
except asyncio.CancelledError:
158154
# Allow CancelledError from asyncio task cancellation to bubble up. Without
@@ -163,28 +159,27 @@ def handle_exceptions(request: Request, debug: bool) -> Generator:
163159
raise
164160
except Exception as exc: # Other error inside method - server error
165161
logging.exception(exc)
166-
handler.response = ExceptionResponse(exc, id=request.id, debug=debug)
162+
handler.response = ExceptionResponse(exc, id=request.id)
167163
finally:
168164
if is_notification(request):
169165
handler.response = NotificationResponse()
170166

171167

172168
def safe_call(
173-
request: Request, methods: Methods, *, debug: bool, extra: Any, serialize: Callable
169+
request: Request, methods: Methods, *, extra: Any, serialize: Callable
174170
) -> Response:
175171
"""
176172
Call a Request, catching exceptions to ensure we always return a Response.
177173
178174
Args:
179175
request: The Request object.
180176
methods: The list of methods that can be called.
181-
debug: Include more information in error responses.
182177
serialize: Function that is used to serialize data.
183178
184179
Returns:
185180
A Response object.
186181
"""
187-
with handle_exceptions(request, debug) as handler:
182+
with handle_exceptions(request) as handler:
188183
if isinstance(request.params, list):
189184
result = call(
190185
lookup(methods, request.method),
@@ -210,7 +205,6 @@ def call_requests(
210205
methods: Methods,
211206
*,
212207
extra: Any,
213-
debug: bool,
214208
serialize: Callable,
215209
) -> Response:
216210
"""
@@ -219,15 +213,12 @@ def call_requests(
219213
Args:
220214
requests: Request object, or a collection of them.
221215
methods: The list of methods that can be called.
222-
debug: Include more information in error responses.
223216
serialize: Function that is used to serialize data.
224217
"""
225218
return (
226219
BatchResponse(
227220
[
228-
safe_call(
229-
r, methods=methods, debug=debug, extra=extra, serialize=serialize
230-
)
221+
safe_call(r, methods=methods, extra=extra, serialize=serialize)
231222
for r in requests
232223
],
233224
serialize_func=serialize,
@@ -236,7 +227,6 @@ def call_requests(
236227
else safe_call(
237228
cast(Request, requests),
238229
methods,
239-
debug=debug,
240230
extra=extra,
241231
serialize=serialize,
242232
)
@@ -277,7 +267,6 @@ def dispatch_pure(
277267
request: str,
278268
methods: Methods,
279269
*,
280-
debug: bool,
281270
extra: Any,
282271
serialize: Callable,
283272
deserialize: Callable,
@@ -292,7 +281,6 @@ def dispatch_pure(
292281
Args:
293282
request: The incoming request string.
294283
methods: Collection of methods that can be called.
295-
debug: Include more information in error responses.
296284
extra: Will be included in the context dictionary passed to methods.
297285
serialize: Function that is used to serialize data.
298286
deserialize: Function that is used to deserialize data.
@@ -302,14 +290,13 @@ def dispatch_pure(
302290
try:
303291
deserialized = validate(deserialize(request), schema)
304292
except JSONDecodeError as exc:
305-
return InvalidJSONResponse(data=str(exc), debug=debug)
293+
return InvalidJSONResponse(data=str(exc))
306294
except ValidationError as exc:
307-
return InvalidJSONRPCResponse(data=None, debug=debug)
295+
return InvalidJSONRPCResponse(data=None)
308296
return call_requests(
309297
create_requests(deserialized),
310298
methods=methods,
311299
extra=extra,
312-
debug=debug,
313300
serialize=serialize,
314301
)
315302

@@ -321,7 +308,6 @@ def dispatch(
321308
*,
322309
basic_logging: bool = False,
323310
extra: Optional[Any] = None,
324-
debug: bool = False,
325311
trim_log_values: bool = False,
326312
serialize: Callable = default_serialize,
327313
deserialize: Callable = default_deserialize,
@@ -338,7 +324,6 @@ def dispatch(
338324
methods: Collection of methods that can be called. If not passed, uses the
339325
internal methods object.
340326
extra: Extra data available inside methods (as context.extra).
341-
debug: Include more information in error responses.
342327
trim_log_values: Show abbreviated requests and responses in log.
343328
serialize: Function that is used to serialize data.
344329
deserialize: Function that is used to deserialize data.
@@ -358,7 +343,6 @@ def dispatch(
358343
response = dispatch_pure(
359344
request,
360345
methods,
361-
debug=debug,
362346
extra=extra,
363347
serialize=serialize,
364348
deserialize=deserialize,

jsonrpcserver/response.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ def __init__(
177177
*args: Any,
178178
code: int,
179179
data: Any = UNSPECIFIED,
180-
debug: bool, # required, named
181180
**kwargs: Any,
182181
) -> None:
183182
"""
@@ -188,22 +187,19 @@ def __init__(
188187
integer.
189188
data: A Primitive or Structured value that contains additional information
190189
about the error. This may be omitted.
191-
debug: Include the data attribute which may include more (possibly
192-
sensitive) information in the response.
193190
"""
194191
super().__init__(*args, **kwargs)
195192
self.code = code
196193
self.message = message
197194
self.data = data
198-
self.debug = debug
199195

200196
def deserialized(self) -> dict:
201197
dct = {
202198
"jsonrpc": "2.0",
203199
"error": {"code": self.code, "message": self.message},
204200
"id": self.id,
205201
} # type: Dict[str, Any]
206-
if self.data is not UNSPECIFIED and self.debug:
202+
if self.data is not UNSPECIFIED:
207203
dct["error"]["data"] = self.data
208204
return dct
209205

0 commit comments

Comments
 (0)