fix bug with empty result and non table result

This commit is contained in:
2025-08-06 19:17:10 +03:00
parent 84dfdd6b35
commit 35cebee819
3 changed files with 16 additions and 9 deletions

View File

@@ -25,6 +25,12 @@ func NewError(code int, message string, data any, id *json.RawMessage) *RPCRespo
}
func NewResponse(result any, id *json.RawMessage) *RPCResponse {
if result == nil {
return &RPCResponse{
JSONRPC: JSONRPCVersion,
ID: id,
}
}
return &RPCResponse{
JSONRPC: JSONRPCVersion,
ID: id,

View File

@@ -28,7 +28,7 @@ func write(nid string, w http.ResponseWriter, msg *RPCResponse) error {
msg.Salt = generateSalt()
if msg.Result != nil {
msg.Checksum = generateChecksum(msg.Result)
} else {
} else if msg.Error != nil {
msg.Checksum = generateChecksum(msg.Error)
}

View File

@@ -149,8 +149,6 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
L.SetField(inTable, "params", paramsTable)
outTable := L.NewTable()
resultTable := L.NewTable()
L.SetField(outTable, "result", resultTable)
L.SetField(inTable, "address", lua.LString(r.RemoteAddr))
L.SetField(sessionMod, "request", inTable)
@@ -518,11 +516,14 @@ func (h *HandlerV1) handleLUA(sid string, r *http.Request, req *rpc.RPCRequest,
}
resultVal := outTbl.RawGetString("result")
payload := make(map[string]any)
if tbl, ok := resultVal.(*lua.LTable); ok {
tbl.ForEach(func(k, v lua.LValue) { payload[k.String()] = ConvertLuaTypesToGolang(v) })
} else {
payload["message"] = ConvertLuaTypesToGolang(resultVal)
if resultVal != lua.LNil {
payload := make(map[string]any)
if tbl, ok := resultVal.(*lua.LTable); ok {
tbl.ForEach(func(k, v lua.LValue) { payload[k.String()] = ConvertLuaTypesToGolang(v) })
} else {
return rpc.NewResponse(ConvertLuaTypesToGolang(resultVal), req.ID)
}
return rpc.NewResponse(payload, req.ID)
}
return rpc.NewResponse(payload, req.ID)
return rpc.NewResponse(nil, req.ID)
}