From af76bce0fa5933a6060d24ca2debaff15e1c7808 Mon Sep 17 00:00:00 2001 From: darosior Date: Fri, 10 Apr 2020 00:47:01 +0200 Subject: [PATCH] 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 --- contrib/plugins/helloworld.py | 6 ++++++ contrib/pyln-client/pyln/client/plugin.py | 9 ++++++--- tests/test_plugin.py | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/contrib/plugins/helloworld.py b/contrib/plugins/helloworld.py index 1a5ede345..3fb15ca0e 100755 --- a/contrib/plugins/helloworld.py +++ b/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") diff --git a/contrib/pyln-client/pyln/client/plugin.py b/contrib/pyln-client/pyln/client/plugin.py index 0446300a6..042b52e63 100644 --- a/contrib/pyln-client/pyln/client/plugin.py +++ b/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): diff --git a/tests/test_plugin.py b/tests/test_plugin.py index f1cbc2e06..5c9ab16c5 100644 --- a/tests/test_plugin.py +++ b/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"""