diff --git a/src/web/RPCServerHandler.h b/src/web/RPCServerHandler.h index a63ef45f..0dbc8000 100644 --- a/src/web/RPCServerHandler.h +++ b/src/web/RPCServerHandler.h @@ -214,28 +214,31 @@ private: // if the result is forwarded - just use it as is // if forwarded request has error, for http, error should be in "result"; for ws, error should // be at top - if (isForwarded && (json.contains("result") || connection->upgraded)) { + if (isForwarded && (json.contains(JS(result)) || connection->upgraded)) { for (auto const& [k, v] : json) response.insert_or_assign(k, v); } else { - response["result"] = json; + response[JS(result)] = json; } // for ws there is an additional field "status" in the response, // otherwise the "status" is in the "result" field if (connection->upgraded) { - auto const id = request.contains("id") ? request.at("id") : nullptr; + auto const appendFieldIfExist = [&](auto const& field) { + if (request.contains(field) and not request.at(field).is_null()) + response[field] = request.at(field); + }; - if (not id.is_null()) - response["id"] = id; + appendFieldIfExist(JS(id)); + appendFieldIfExist(JS(api_version)); - if (!response.contains("error")) - response["status"] = "success"; + if (!response.contains(JS(error))) + response[JS(status)] = JS(success); - response["type"] = "response"; + response[JS(type)] = JS(response); } else { - if (response.contains("result") && !response["result"].as_object().contains("error")) - response["result"].as_object()["status"] = "success"; + if (response.contains(JS(result)) && !response[JS(result)].as_object().contains(JS(error))) + response[JS(result)].as_object()[JS(status)] = JS(success); } } diff --git a/src/web/impl/ErrorHandling.h b/src/web/impl/ErrorHandling.h index a7f0a764..b58915ad 100644 --- a/src/web/impl/ErrorHandling.h +++ b/src/web/impl/ErrorHandling.h @@ -150,18 +150,23 @@ public: auto e = rpc::makeError(error); if (request_) { - auto const& req = request_.value(); - auto const id = req.contains("id") ? req.at("id") : nullptr; - if (not id.is_null()) - e["id"] = id; + auto const appendFieldIfExist = [&](auto const& field) { + if (request_->contains(field) and not request_->at(field).is_null()) + e[field] = request_->at(field); + }; - e["request"] = req; + appendFieldIfExist(JS(id)); + + if (connection_->upgraded) + appendFieldIfExist(JS(api_version)); + + e[JS(request)] = request_.value(); } if (connection_->upgraded) { return e; } - return {{"result", e}}; + return {{JS(result), e}}; } }; diff --git a/unittests/web/RPCServerHandlerTests.cpp b/unittests/web/RPCServerHandlerTests.cpp index 05275dd5..4a864c93 100644 --- a/unittests/web/RPCServerHandlerTests.cpp +++ b/unittests/web/RPCServerHandlerTests.cpp @@ -129,7 +129,8 @@ TEST_F(WebRPCServerHandlerTest, WsNormalPath) session->upgraded = true; static auto constexpr request = R"({ "command": "server_info", - "id": 99 + "id": 99, + "api_version": 2 })"; backend->setRange(MINSEQ, MAXSEQ); @@ -140,6 +141,7 @@ TEST_F(WebRPCServerHandlerTest, WsNormalPath) "id": 99, "status": "success", "type": "response", + "api_version": 2, "warnings": [ { "id": 2001, @@ -291,10 +293,12 @@ TEST_F(WebRPCServerHandlerTest, WsErrorPath) "error_message": "ledgerIndexMalformed", "status": "error", "type": "response", + "api_version": 2, "request": { "command": "ledger", "ledger_index": "xx", - "id": "123" + "id": "123", + "api_version": 2 }, "warnings": [ { @@ -309,7 +313,8 @@ TEST_F(WebRPCServerHandlerTest, WsErrorPath) static auto constexpr requestJSON = R"({ "command": "ledger", "ledger_index": "xx", - "id": "123" + "id": "123", + "api_version": 2 })"; EXPECT_CALL(*rpcEngine, buildResponse(testing::_)) .WillOnce(testing::Return(rpc::Status{rpc::RippledError::rpcINVALID_PARAMS, "ledgerIndexMalformed"}));