lua_func: nil return is not an error
_handle_request was using the 1st pcall return as the error discriminator, so any Lua function returning nothing (e.g. long_wait in the example) was reported as failed. Discriminate on the 2nd return (err) instead, and encode nil result as cjson.null so the returned_value field stays present in the JSON-RPC response.
This commit is contained in:
@@ -41,8 +41,7 @@ end
|
|||||||
--- INTERNAL: Handle requests from the client
|
--- INTERNAL: Handle requests from the client
|
||||||
function JSONRPC:_handle_request(req)
|
function JSONRPC:_handle_request(req)
|
||||||
local method = self.methods[req.method]
|
local method = self.methods[req.method]
|
||||||
local ok, ret
|
local ok, ret, err
|
||||||
local res, err
|
|
||||||
if not method then
|
if not method then
|
||||||
if req.id then self:_send_error(req.id, string.format("Method '%s' not registered in lua server")) end
|
if req.id then self:_send_error(req.id, string.format("Method '%s' not registered in lua server")) end
|
||||||
return
|
return
|
||||||
@@ -52,15 +51,18 @@ function JSONRPC:_handle_request(req)
|
|||||||
|
|
||||||
-- Only send response if it's not a Notification (notifications have no ID)
|
-- Only send response if it's not a Notification (notifications have no ID)
|
||||||
if req.id then
|
if req.id then
|
||||||
if ok then
|
if not ok then
|
||||||
res = ret
|
-- pcall trapped a runtime error in the method itself.
|
||||||
if res == nil then
|
self:_send_error(req.id, tostring(ret))
|
||||||
self:_send_error(req.id, tostring(err))
|
elseif err ~= nil then
|
||||||
else
|
-- Method ran but signaled a logical error via its 2nd return.
|
||||||
self:_send({ jsonrpc = "2.0", result = { returned_value = res }, id = req.id })
|
|
||||||
end
|
|
||||||
else
|
|
||||||
self:_send_error(req.id, tostring(err))
|
self:_send_error(req.id, tostring(err))
|
||||||
|
else
|
||||||
|
-- Success. A user function returning nothing yields ret==nil;
|
||||||
|
-- encode it as JSON null so "returned_value" stays present.
|
||||||
|
local val = ret
|
||||||
|
if val == nil then val = json.null end
|
||||||
|
self:_send({ jsonrpc = "2.0", result = { returned_value = val }, id = req.id })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user