Browse Source

pyln-client: send proper JSONRPC2 errors on failed call

Changelog-Added: pyln now sends proper error on bad calls to plugin methods

Co-Authored-By: Sergi Delgado Segura <sergi.delgado.s@gmail.com>
travis-debug
darosior 5 years ago
committed by Rusty Russell
parent
commit
af76bce0fa
  1. 6
      contrib/plugins/helloworld.py
  2. 9
      contrib/pyln-client/pyln/client/plugin.py
  3. 5
      tests/test_plugin.py

6
contrib/plugins/helloworld.py

@ -19,6 +19,12 @@ def hello(plugin, name="world"):
return s
@plugin.method("bye")
def bye(plugin, name, **kwargs):
"""This methods requires {name} to be set by the caller !"""
return "Bye {}".format(name)
@plugin.init()
def init(options, configuration, plugin, **kwargs):
plugin.log("Plugin helloworld.py initialized")

9
contrib/pyln-client/pyln/client/plugin.py

@ -83,9 +83,12 @@ class Request(dict):
self._write_result({
'jsonrpc': '2.0',
'id': self.id,
"error": "Error while processing {method}: {exc}".format(
method=self.method, exc=repr(exc)
),
"error": {
"code": -32600, # "Invalid Request"
"message": "Error while processing {method}: {exc}"
.format(method=self.method, exc=str(exc)),
# 'data' field "may be omitted."
},
})
def _write_result(self, result):

5
tests/test_plugin.py

@ -180,6 +180,11 @@ def test_rpc_passthrough(node_factory):
with pytest.raises(RpcError):
n.rpc.fail()
# Try to call a method without enough arguments
with pytest.raises(RpcError, match="processing bye: missing a required"
" argument"):
n.rpc.bye()
def test_plugin_dir(node_factory):
"""--plugin-dir works"""

Loading…
Cancel
Save