Make sure that handlers always return Json::objectValue.

This commit is contained in:
Tom Ritchford
2014-12-01 15:26:59 -05:00
parent b0d47ebcc6
commit 86df482842
22 changed files with 43 additions and 32 deletions

View File

@@ -101,6 +101,7 @@ public:
void filterNodes (std::vector<SHAMapNodeID>& nodeIDs, std::vector<uint256>& nodeHashes, void filterNodes (std::vector<SHAMapNodeID>& nodeIDs, std::vector<uint256>& nodeHashes,
std::set<SHAMapNodeID>& recentNodes, int max, bool aggressive); std::set<SHAMapNodeID>& recentNodes, int max, bool aggressive);
/** Return a Json::objectValue. */
Json::Value getJson (int); Json::Value getJson (int);
void runData (); void runData ();

View File

@@ -144,6 +144,8 @@ public:
virtual void reportValidations (const AmendmentSet&) = 0; virtual void reportValidations (const AmendmentSet&) = 0;
virtual Json::Value getJson (int) = 0; virtual Json::Value getJson (int) = 0;
/** Returns a Json::objectValue. */
virtual Json::Value getJson (uint256 const& ) = 0; virtual Json::Value getJson (uint256 const& ) = 0;
virtual void virtual void

View File

@@ -24,7 +24,7 @@
namespace ripple { namespace ripple {
/** A PropertyStream::Sink which produces a Json::Value. */ /** A PropertyStream::Sink which produces a Json::Value of type objectValue. */
class JsonPropertyStream : public beast::PropertyStream class JsonPropertyStream : public beast::PropertyStream
{ {
public: public:
@@ -65,4 +65,3 @@ protected:
} }
#endif #endif

View File

@@ -34,6 +34,7 @@ namespace jss {
// TODO Move the string not part of the JSON-RPC API into another file // TODO Move the string not part of the JSON-RPC API into another file
JSS ( accepted ); JSS ( accepted );
JSS ( account ); JSS ( account );
JSS ( accounts );
JSS ( account_hash ); JSS ( account_hash );
JSS ( account_index ); JSS ( account_index );
JSS ( accountState ); JSS ( accountState );

View File

@@ -239,6 +239,7 @@ public:
return getJson (warningThreshold); return getJson (warningThreshold);
} }
/** Returns a Json::objectValue. */
Json::Value getJson (int threshold) Json::Value getJson (int threshold)
{ {
clock_type::time_point const now (m_clock.now()); clock_type::time_point const now (m_clock.now());

View File

@@ -68,7 +68,7 @@ Json::Value doAccountInfo (RPC::Context& context)
else else
{ {
result["account"] = naAccount.humanAccountID (); result["account"] = naAccount.humanAccountID ();
result = rpcError (rpcACT_NOT_FOUND, result); RPC::inject_error (rpcACT_NOT_FOUND, result);
} }
return result; return result;

View File

@@ -54,7 +54,7 @@ Json::Value doConnect (RPC::Context& context)
if (! is_unspecified (ip)) if (! is_unspecified (ip))
getApp().overlay ().connect (ip.at_port(iPort)); getApp().overlay ().connect (ip.at_port(iPort));
return "connecting"; return RPC::makeObjectValue ("connecting");
} }
} // ripple } // ripple

View File

@@ -23,7 +23,7 @@ namespace ripple {
Json::Value doLedgerCleaner (RPC::Context& context) Json::Value doLedgerCleaner (RPC::Context& context)
{ {
getApp().getLedgerMaster().doLedgerCleaner (context.params); getApp().getLedgerMaster().doLedgerCleaner (context.params);
return "Cleaner configured"; return RPC::makeObjectValue ("Cleaner configured");
} }

View File

@@ -29,7 +29,7 @@ Json::Value doSMS (RPC::Context& context)
HTTPClient::sendSMS ( HTTPClient::sendSMS (
getApp().getIOService (), context.params["text"].asString ()); getApp().getIOService (), context.params["text"].asString ());
return "sms dispatched"; return RPC::makeObjectValue ("sms dispatched");
} }
} // ripple } // ripple

View File

@@ -25,7 +25,7 @@ Json::Value doStop (RPC::Context& context)
auto lock = getApp().masterLock(); auto lock = getApp().masterLock();
getApp().signalStop (); getApp().signalStop ();
return SYSTEM_NAME " server stopping"; return RPC::makeObjectValue (SYSTEM_NAME " server stopping");
} }
} // ripple } // ripple

View File

@@ -39,13 +39,13 @@ Json::Value doUnlAdd (RPC::Context& context)
{ {
getApp().getUNL ().nodeAddPublic ( getApp().getUNL ().nodeAddPublic (
raNodePublic, UniqueNodeList::vsManual, strComment); raNodePublic, UniqueNodeList::vsManual, strComment);
return "adding node by public key"; return RPC::makeObjectValue ("adding node by public key");
} }
else else
{ {
getApp().getUNL ().nodeAddDomain ( getApp().getUNL ().nodeAddDomain (
strNode, UniqueNodeList::vsManual, strComment); strNode, UniqueNodeList::vsManual, strComment);
return "adding node by domain"; return RPC::makeObjectValue ("adding node by domain");
} }
} }

View File

@@ -36,12 +36,12 @@ Json::Value doUnlDelete (RPC::Context& context)
if (raNodePublic.setNodePublic (strNode)) if (raNodePublic.setNodePublic (strNode))
{ {
getApp().getUNL ().nodeRemovePublic (raNodePublic); getApp().getUNL ().nodeRemovePublic (raNodePublic);
return "removing node by public key"; return RPC::makeObjectValue ("removing node by public key");
} }
else else
{ {
getApp().getUNL ().nodeRemoveDomain (strNode); getApp().getUNL ().nodeRemoveDomain (strNode);
return "removing node by domain"; return RPC::makeObjectValue ("removing node by domain");
} }
} }

View File

@@ -31,7 +31,7 @@ Json::Value doUnlLoad (RPC::Context& context)
return rpcError (rpcLOAD_FAILED); return rpcError (rpcLOAD_FAILED);
} }
return "loading"; return RPC::makeObjectValue ("loading");
} }
} // ripple } // ripple

View File

@@ -26,7 +26,7 @@ Json::Value doUnlNetwork (RPC::Context& context)
auto lock = getApp().masterLock(); auto lock = getApp().masterLock();
getApp().getUNL ().nodeNetwork (); getApp().getUNL ().nodeNetwork ();
return "fetching"; return RPC::makeObjectValue ("fetching");
} }
} // ripple } // ripple

View File

@@ -25,7 +25,7 @@ Json::Value doUnlReset (RPC::Context& context)
auto lock = getApp().masterLock(); auto lock = getApp().masterLock();
getApp().getUNL ().nodeReset (); getApp().getUNL ().nodeReset ();
return "removing nodes"; return RPC::makeObjectValue ("removing nodes");
} }
} // ripple } // ripple

View File

@@ -26,7 +26,7 @@ Json::Value doUnlScore (RPC::Context& context)
auto lock = getApp().masterLock(); auto lock = getApp().masterLock();
getApp().getUNL ().nodeScore (); getApp().getUNL ().nodeScore ();
return "scoring requested"; return RPC::makeObjectValue ("scoring requested");
} }
} // ripple } // ripple

View File

@@ -60,18 +60,14 @@ Json::Value doWalletAccounts (RPC::Context& context)
if (!ret.empty ()) if (!ret.empty ())
return ret; return ret;
ret["accounts"] ret[jss::accounts]
= RPC::accounts (ledger, naMasterGenerator, context.netOps); = RPC::accounts (ledger, naMasterGenerator, context.netOps);
return ret; return ret;
} }
else else
{ {
// Had accounts via seed as master, return them. // Had accounts via seed as master, return them.
Json::Value ret (Json::objectValue); return RPC::makeObjectValue (jsonAccounts, jss::accounts);
ret["accounts"] = jsonAccounts;
return ret;
} }
} }

View File

@@ -25,6 +25,8 @@ namespace RPC {
// --> strIdent: public key, account ID, or regular seed. // --> strIdent: public key, account ID, or regular seed.
// --> bStrict: Only allow account id or public key. // --> bStrict: Only allow account id or public key.
// <-- bIndex: true if iIndex > 0 and used the index. // <-- bIndex: true if iIndex > 0 and used the index.
//
// Returns a Json::objectValue, containing error information if there was one.
Json::Value accountFromString ( Json::Value accountFromString (
Ledger::ref lrLedger, Ledger::ref lrLedger,
RippleAddress& naAccount, RippleAddress& naAccount,

View File

@@ -46,6 +46,16 @@ struct Handler
const Handler* getHandler(std::string name); const Handler* getHandler(std::string name);
/** Return a Json::objectValue with a single entry. */
template <class Value>
Json::Value makeObjectValue (
Value const& value, Json::StaticString const& field = jss::message)
{
Json::Value result (Json::objectValue);
result[field] = value;
return result;
}
} // RPC } // RPC
} // ripple } // ripple

View File

@@ -40,6 +40,11 @@ static const int LEDGER_VALIDATED = -3;
// //
// In the absence of the "ledger_hash" or "ledger_index" parameters, the code // In the absence of the "ledger_hash" or "ledger_index" parameters, the code
// assumes that "ledger_index" has the value "current". // assumes that "ledger_index" has the value "current".
//
// Returns a Json::objectValue. If there was an error, it will be in that
// return value. Otherwise, the object contains the field "validated" and
// optionally the fields "ledger_hash", "ledger_index" and
// "ledger_current_index", if they are defined.
Json::Value lookupLedger ( Json::Value lookupLedger (
Json::Value const& params, Json::Value const& params,
Ledger::pointer& ledger, Ledger::pointer& ledger,

View File

@@ -140,17 +140,9 @@ Json::Value RPCHandler::doCommand (
LoadEvent::autoptr ev = getApp().getJobQueue().getLoadEventAP( LoadEvent::autoptr ev = getApp().getJobQueue().getLoadEventAP(
jtGENERIC, "cmd:" + strCommand); jtGENERIC, "cmd:" + strCommand);
RPC::Context context {params, loadType, netOps_, infoSub_, role_}; RPC::Context context {params, loadType, netOps_, infoSub_, role_};
Json::Value jvRaw = handler->method_(context); auto result = handler->method_(context);
assert (result.isObject());
// Regularize result. return result;
if (jvRaw.isObject ())
return jvRaw;
// Probably got a string.
Json::Value jvResult (Json::objectValue);
jvResult[jss::message] = jvRaw;
return jvResult;
} }
catch (std::exception& e) catch (std::exception& e)
{ {

View File

@@ -100,6 +100,8 @@ public:
} // namespace RPCDetail } // namespace RPCDetail
/** Returns a Json::objectValue. */
Json::Value transactionSign ( Json::Value transactionSign (
Json::Value params, Json::Value params,
bool bSubmit, bool bSubmit,