jsonrpcserver: send exception message instead of "Server error"
When RPC methods raises an exception there is always the same reply returned:
INFO jsonrpcclient.client.response: {"jsonrpc": "2.0", "error": {"code": -32000, "message": "Server error"}, "id": 2}
It would be great if ‘message’ could contain str(exception) that was thrown by called method.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 19 (10 by maintainers)
I would like to implement application defined errors with specific code, message and data (https://www.jsonrpc.org/specification#response_object). So something like
{"jsonrpc": "2.0", "error": {"code": -32800, "message": "Invalid input geometry", "data": {"index": 33, "coordinates": [1135.124, -25689]}}, "id": "1"}I would like to raise application defined exception in rpc method that I would handle in the way shown above. I am working with geo data that are not always perfect.I have the same issue as @seidlmic.
One approach would be to raise a ExceptionResponse from inside the method. This way, all ‘specially handled’ exceptions will get the proper response, and ‘normal’ exceptions get the generic jsonrpc exception handling.
To make this happen, we only have to adjust the handle_exceptions call to catch ExceptionResponses and rethrow them.
Also could you reopen this issue? It took me a while to find this.
We could give the option to disable exception handling, so the user could handle them
@bcb I agree, there should be a catchall that just logs the exception and responds with a generic “Server exception” without too much detail.
Apart from that I can imagine that API functions can raise an “ApiException” with a message, code and optionally more details that can be used for custom, application-specific errors. In that way, library users can actually use the error signaling mechanisms of the json-rpc protocol.
I’ll hack some code and open a PR 😃