mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-20 07:09:15 +01:00
rpc: refactor single/batch requests
Simplify the request handling flow so that errors and results only come from JSONRPCExec()
This commit is contained in:
committed by
Ryan Ofsky
parent
df6e3756d6
commit
a64a2b77e0
@@ -358,36 +358,13 @@ bool IsDeprecatedRPCEnabled(const std::string& method)
|
||||
return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
|
||||
}
|
||||
|
||||
static UniValue JSONRPCExecOne(JSONRPCRequest jreq, const UniValue& req)
|
||||
UniValue JSONRPCExec(const JSONRPCRequest& jreq)
|
||||
{
|
||||
UniValue rpc_result(UniValue::VOBJ);
|
||||
// Might throw exception. Single requests will throw and send HTTP error codes
|
||||
// but inside a batch, we just include the error object and return HTTP 200
|
||||
UniValue result = tableRPC.execute(jreq);
|
||||
|
||||
try {
|
||||
jreq.parse(req);
|
||||
|
||||
UniValue result = tableRPC.execute(jreq);
|
||||
rpc_result = JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id);
|
||||
}
|
||||
catch (UniValue& e)
|
||||
{
|
||||
rpc_result = JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
rpc_result = JSONRPCReplyObj(NullUniValue,
|
||||
JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
|
||||
}
|
||||
|
||||
return rpc_result;
|
||||
}
|
||||
|
||||
std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq)
|
||||
{
|
||||
UniValue ret(UniValue::VARR);
|
||||
for (unsigned int reqIdx = 0; reqIdx < vReq.size(); reqIdx++)
|
||||
ret.push_back(JSONRPCExecOne(jreq, vReq[reqIdx]));
|
||||
|
||||
return ret.write() + "\n";
|
||||
return JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user