Skip to content

Commit

Permalink
pyln-client: send proper JSONRPC2 errors on failed call
Browse files Browse the repository at this point in the history
Changelog-Added: pyln now sends proper error on bad calls to plugin methods

Co-Authored-By: Sergi Delgado Segura <[email protected]>
  • Loading branch information
darosior and sr-gi committed Apr 10, 2020
1 parent b68066e commit ed86321
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions contrib/plugins/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 6 additions & 3 deletions contrib/pyln-client/pyln/client/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ def set_exception(self, exc):
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):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit ed86321

Please sign in to comment.