mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 07:34:24 +01:00
plugin: The example plugin can now return errors
Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
committed by
Rusty Russell
parent
1e139e412b
commit
0f72e0bce8
@@ -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)
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
result = None
|
result = None
|
||||||
method = methods[request['method']]
|
method = methods[request['method']]
|
||||||
params = request['params']
|
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']
|
||||||
}
|
}
|
||||||
|
except Exception as e:
|
||||||
|
result = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"error": "Error while processing {}".format(request['method']),
|
||||||
|
"id": request['id']
|
||||||
|
}
|
||||||
|
|
||||||
json.dump(result, fp=sys.stdout)
|
json.dump(result, fp=sys.stdout)
|
||||||
sys.stdout.write('\n')
|
sys.stdout.write('\n')
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
partial = ""
|
partial = ""
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|||||||
Reference in New Issue
Block a user