rpc: Avoid returning HTTP errors for JSON-RPC 2.0 requests

Avoid returning HTTP status errors for non-batch JSON-RPC 2.0 requests if the
RPC method failed but the HTTP request was otherwise valid. Batch requests
already did not return HTTP errors previously.
This commit is contained in:
Matthew Zipkin
2023-07-07 14:41:23 -04:00
parent 466b90562f
commit bf1a1f1662
4 changed files with 30 additions and 12 deletions

View File

@@ -358,11 +358,20 @@ bool IsDeprecatedRPCEnabled(const std::string& method)
return find(enabled_methods.begin(), enabled_methods.end(), method) != enabled_methods.end();
}
UniValue JSONRPCExec(const JSONRPCRequest& jreq)
UniValue JSONRPCExec(const JSONRPCRequest& jreq, bool catch_errors)
{
// 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);
UniValue result;
if (catch_errors) {
try {
result = tableRPC.execute(jreq);
} catch (UniValue& e) {
return JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id, jreq.m_json_version);
} catch (const std::exception& e) {
return JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_MISC_ERROR, e.what()), jreq.id, jreq.m_json_version);
}
} else {
result = tableRPC.execute(jreq);
}
return JSONRPCReplyObj(std::move(result), NullUniValue, jreq.id, jreq.m_json_version);
}