From 0f72e0bce825e851df4b84fd75be64541f5d46f0 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 26 Nov 2018 19:47:52 +0100 Subject: [PATCH] plugin: The example plugin can now return errors Signed-off-by: Christian Decker --- contrib/plugins/helloworld.py | 39 +++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/contrib/plugins/helloworld.py b/contrib/plugins/helloworld.py index 6ae5b1ff2..6b5bba40e 100755 --- a/contrib/plugins/helloworld.py +++ b/contrib/plugins/helloworld.py @@ -19,6 +19,10 @@ def json_hello(request, name): return greeting +def json_fail(request): + raise ValueError("This will fail") + + def json_getmanifest(request): global greeting return { @@ -33,6 +37,10 @@ def json_getmanifest(request): "name": "hello", "description": "Returns a personalized greeting for {name}", }, + { + "name": "fail", + "description": "Always returns a failure for testing", + }, ] } @@ -48,6 +56,7 @@ def json_init(request, options): methods = { 'hello': json_hello, + 'fail': json_fail, 'getmanifest': json_getmanifest, 'init': json_init, } @@ -55,25 +64,33 @@ methods = { partial = "" for l in sys.stdin: - partial += l try: + partial += l request = json.loads(partial) - result = None - method = methods[request['method']] - params = request['params'] + except Exception: + continue + + result = None + method = methods[request['method']] + params = request['params'] + try: if isinstance(params, dict): result = method(request, **params) else: result = method(request, *params) - result = { "jsonrpc": "2.0", "result": result, "id": request['id'] } - json.dump(result, fp=sys.stdout) - sys.stdout.write('\n') - sys.stdout.flush() - partial = "" - except Exception: - pass + except Exception as e: + result = { + "jsonrpc": "2.0", + "error": "Error while processing {}".format(request['method']), + "id": request['id'] + } + + json.dump(result, fp=sys.stdout) + sys.stdout.write('\n') + sys.stdout.flush() + partial = ""