mbr / tinyrpc

A compact, modular transport and protocol agnostic RPC library. Does jsonrpc v2.
https://tinyrpc.readthedocs.org
MIT License
156 stars 53 forks source link

Incorrect error code if method is called with wrong number of arguments #19

Closed jnnk closed 7 years ago

jnnk commented 9 years ago

If a JSON RPC method is called with the wrong number of arguments, "invalid params" (error code -32602) should be replied (http://www.jsonrpc.org/specification#error_object). At the moment, however, the signature isn't checked. Thus, a simple TypeError is raised when the method is called, leading to a generic "server error" (code -32000).

The hack I use at the moment looks like this:

import inspect
from tinyrpc.dispatch import public as public_

def public(f):
    def new_f(*args, **kwargs):
        try:
            inspect.getcallargs(f, *args, **kwargs)
        except TypeError:
            raise JSONRPCInvalidParamsError()
        else:
            return f(*args, **kwargs)
    new_f.func_name = f.func_name
    new_f.func_doc = f.func_doc
    return public_(new_f)

This extends the default public decorator in such a way that it raises JSONRPCInvalidParamsError if necessary. This is not the right place for doing this, though, and I'd like it better if this would be done somewhere inside tinyrpc. Let me know, if I overlooked something or there's a better way of doing this.

lnoor commented 7 years ago

Thank you for your contribution. It will become part of the next release in a couple of weeks.