Browse Source

plugin: The example plugin can now return errors

Signed-off-by: Christian Decker <decker.christian@gmail.com>
plugin-3
Christian Decker 6 years ago
parent
commit
cc04d7eaa2
No known key found for this signature in database GPG Key ID: 1416D83DC4F0E86D
  1. 39
      contrib/plugins/helloworld.py

39
contrib/plugins/helloworld.py

@ -19,6 +19,10 @@ def json_hello(request, name):
return greeting return greeting
def json_fail(request):
raise ValueError("This will fail")
def json_getmanifest(request): def json_getmanifest(request):
global greeting global greeting
return { return {
@ -33,6 +37,10 @@ def json_getmanifest(request):
"name": "hello", "name": "hello",
"description": "Returns a personalized greeting for {name}", "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 = { methods = {
'hello': json_hello, 'hello': json_hello,
'fail': json_fail,
'getmanifest': json_getmanifest, 'getmanifest': json_getmanifest,
'init': json_init, 'init': json_init,
} }
@ -55,25 +64,33 @@ methods = {
partial = "" partial = ""
for l in sys.stdin: for l in sys.stdin:
partial += l
try: try:
partial += l
request = json.loads(partial) request = json.loads(partial)
result = None except Exception:
method = methods[request['method']] continue
params = request['params']
result = None
method = methods[request['method']]
params = request['params']
try:
if isinstance(params, dict): if isinstance(params, dict):
result = method(request, **params) result = method(request, **params)
else: else:
result = method(request, *params) result = method(request, *params)
result = { result = {
"jsonrpc": "2.0", "jsonrpc": "2.0",
"result": result, "result": result,
"id": request['id'] "id": request['id']
} }
json.dump(result, fp=sys.stdout) except Exception as e:
sys.stdout.write('\n') result = {
sys.stdout.flush() "jsonrpc": "2.0",
partial = "" "error": "Error while processing {}".format(request['method']),
except Exception: "id": request['id']
pass }
json.dump(result, fp=sys.stdout)
sys.stdout.write('\n')
sys.stdout.flush()
partial = ""

Loading…
Cancel
Save