samuraisam / django-json-rpc

JSON-RPC Implementation for Django
MIT License
286 stars 83 forks source link

Return customized error message directly #72

Open Georgi87 opened 9 years ago

Georgi87 commented 9 years ago

I have a function, which may return an error state:

@jsonrpc_method('eth_sendRawTransaction')
def eth_sendRawTransaction(request, arg1):
    if arg1 == 1:
        return "This worked"
    else:
        raise Error("This is an error")

The response I would like to get back is the following:

{"error": {"message": "This is an error"}, "jsonrpc": "2.0", "id": 7}

Instead I get a nested error message:

{"error": 
{"executable": "/home/vagrant/.pyenv/versions/2.7.9/bin/python", 
"code": 0, 
"name": "Error", 
"message": "Error: This is an error", 
"data": null, 
"stack": "Traceback (most recent call last):\n  File \"/home/vagrant/.pyenv/versions/2.7.9/lib/python2.7/site-packages/jsonrpc/site.py\", line 162, in response_dict\n    R = apply_version[version](method, request, D['params'])\n  File \"/home/vagrant/.pyenv/versions/2.7.9/lib/python2.7/site-packages/jsonrpc/site.py\", line 125, in <lambda>\n    apply_version = {'2.0': lambda f, r, p: f(r, **encode_kw(p)) if type(p) is dict else f(r, *p),\n  File \"/home/vagrant/.pyenv/versions/2.7.9/lib/python2.7/site-packages/jsonrpc/__init__.py\", line 242, in exc_printer\n    six.reraise(*sys.exc_info())\n  File \"/home/vagrant/.pyenv/versions/2.7.9/lib/python2.7/site-packages/jsonrpc/__init__.py\", line 234, in exc_printer\n    return _func(*a, **kw)\n  File \"/vagrant/factserver/eth/views.py\", line 22, in eth_sendRawTransaction\n    raise Error(response[\"error\"])\nError\n"}, 
"jsonrpc": "2.0", 
"id": 7}