json-rpc
json-rpc copied to clipboard
On certain TypeErrors, json-rpc fails to log and responds incorrectly
If application code raises a TypeError, json-rpc doesn't necessarily log the exception and stack trace. Instead, it responds with an "Invalid params" error with code "-32602".
This is also not a correct message in this case because the reason for the error is not invalid parameters sent by the caller. It is an error in application code.
I believe this issue is due to the exception handling here, where the code bypasses exception logging if the exception has type TypeError
. Or perhaps it is due to incorrect logic in is_invalid_params()
.
Suggestion: instead of the tricky logic in is_invalid_params()
, is there any reason you couldn't do something like inspect the depth / level of the stack trace to see where the error is originating? It seems like that may be simpler, or more importantly more robust.
Another option could be to, inside is_invalid_params()
, create a function on the fly with the same signature as obtained by inspect.getargspec(func)
but with empty body pass
. Then you could invoke the function and see if an error occurs. That way you will be using the same rules that Python uses.
@cjerdonek application is supposed to raise exceptions, inherited from JSONRPCDispatchException
, so the manager could show a proper message. If an exception is not specified, then here would be a problem as you mentioned. is_invalid_params()
could use inspect.getargspec(func)
. Any pull requests would be highly appreciated.
@cjerdonek application is supposed to raise exceptions, inherited from JSONRPCDispatchException, so the manager could show a proper message.
Okay, I was more referring to the case that arises if there is a bug in the application code, in which case the developer doesn't have control over the exception type. In this case, this issue is causing json-rpc's error handling not to help the developer as much in diagnosing the source of the bug. However, this may be addressable by providing a "catch-all" error handler in the application's code and re-raising with an exception type that json-rpc knows how to handle (so that json-rpc's logging kicks in).
Also, by the way, I believe that the assumptions being checked in is_invalid_params()
are failing here and here. If func
has signature func(*args, **kwargs)
, then len(funcargs)
will be zero, which will cause the check to fail if the args
or kwargs
provided by the caller is non-empty.
Was anything decided regarding this? I am running into the same issue, where a TypeError
in the application code is being caught and returned as a "Invalid params" error. Our function has a func(*args, **kwargs)
signature and hence is_invalid_params()
is returning True
when it shouldn't.
Sorry for misuse the issue system as a chat ..
..is supposed to raise exceptions, inherited from
JSONRPCDispatchException
, so the manager could show a proper message.
Question to JSONRPCDispatchException : When I try to use it inside a dispatcher method, I always get a NameError. What I'm doing wrong??
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/jsonrpc/manager.py", line 112, in _get_responses
result = method(*request.args, **request.kwargs)
File "webAPIEmulator.py", line 123, in read_registry
raise jsonrpc.exceptions.JSONRPCDispatchException( -29999, "key not found")
NameError: name 'jsonrpc' is not defined