| 224 | } |
| 225 | |
| 226 | int CommandLineRPC(int argc, char* argv[]) |
| 227 | { |
| 228 | string strPrint; |
| 229 | int nRet = 0; |
| 230 | try { |
| 231 | // Skip switches |
| 232 | while (argc > 1 && IsSwitchChar(argv[1][0])) { |
| 233 | argc--; |
| 234 | argv++; |
| 235 | } |
| 236 | |
| 237 | // Method |
| 238 | if (argc < 2) |
| 239 | throw runtime_error("too few parameters"); |
| 240 | string strMethod = argv[1]; |
| 241 | |
| 242 | // Parameters default to strings |
| 243 | std::vector<std::string> strParams(&argv[2], &argv[argc]); |
| 244 | UniValue params = RPCConvertValues(strMethod, strParams); |
| 245 | |
| 246 | // Execute and handle connection failures with -rpcwait |
| 247 | const bool fWait = GetBoolArg("-rpcwait", false); |
| 248 | do { |
| 249 | try { |
| 250 | const UniValue reply = CallRPC(strMethod, params); |
| 251 | |
| 252 | // Parse reply |
| 253 | const UniValue& result = find_value(reply, "result"); |
| 254 | const UniValue& error = find_value(reply, "error"); |
| 255 | |
| 256 | if (!error.isNull()) { |
| 257 | // Error |
| 258 | int code = error["code"].get_int(); |
| 259 | if (fWait && code == RPC_IN_WARMUP) |
| 260 | throw CConnectionFailed("server in warmup"); |
| 261 | strPrint = "error: " + error.write(); |
| 262 | nRet = abs(code); |
| 263 | if (error.isObject() && GetBoolArg("-printstringerror", false)) |
| 264 | { |
| 265 | UniValue errCode = find_value(error, "code"); |
| 266 | UniValue errMsg = find_value(error, "message"); |
| 267 | strPrint = errCode.isNull() ? "" : "error code: "+errCode.getValStr()+"\n"; |
| 268 | |
| 269 | if (errMsg.isStr()) |
| 270 | strPrint += "error message:\n"+errMsg.get_str(); |
| 271 | } |
| 272 | } else { |
| 273 | // Result |
| 274 | if (result.isNull()) |
| 275 | strPrint = ""; |
| 276 | else if (result.isStr()) |
| 277 | strPrint = result.get_str(); |
| 278 | else |
| 279 | strPrint = result.write(2); |
| 280 | } |
| 281 | |
| 282 | // Connection succeeded, no need to retry. |
| 283 | break; |
no test coverage detected