ajsonrpc icon indicating copy to clipboard operation
ajsonrpc copied to clipboard

AsyncJSONRPCResponseManager returns JSONRPC20InvalidParams instead JSONRPC20ServerError when exceptions occur

Open ChamalAB opened this issue 1 year ago • 0 comments

Tested in python 3.11.3

import asyncio
import json

from ajsonrpc import AsyncJSONRPCResponseManager, Dispatcher

class ExampleClass:
    @classmethod
    def example_one(cls, a, b):
        raise Exception("Test error")
        return a * b


def example_two(**kwargs):
    raise Exception("Test error")
    return kwargs['a'] * kwargs['b'] 


dispatcher = Dispatcher()
dispatcher.add_class(ExampleClass)
dispatcher.add_function(example_two)


manager = AsyncJSONRPCResponseManager(dispatcher=dispatcher,is_server_error_verbose=True)

async def run():
    payload = {
        'jsonrpc': '2.0',
        'method': 'exampleclass.example_one',
        'params': [2, 3],
        'id': 1
    }
    response = await manager.get_response_for_payload(json.dumps(payload))
    print(response.body)


    payload = {
        'jsonrpc': '2.0',
        'method': 'example_two',
        'params': {'a': 2, 'b': 3},
        'id': 2
    }
    response = await manager.get_response_for_payload(json.dumps(payload))
    print(response.body)


asyncio.run(run())

Output

{'jsonrpc': '2.0', 'id': 1, 'error': {'code': -32602, 'message': 'Invalid params'}}
{'jsonrpc': '2.0', 'id': 2, 'error': {'code': -32602, 'message': 'Invalid params'}}

ChamalAB avatar Jul 04 '23 05:07 ChamalAB