Skip to content

Commit

Permalink
Merge pull request #1908 from laanwj/2012_10_rpcerrors
Browse files Browse the repository at this point in the history
Document RPC error codes
  • Loading branch information
sipa committed Oct 5, 2012
2 parents c0b130b + 285746d commit 39930ca
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 112 deletions.
66 changes: 33 additions & 33 deletions src/bitcoinrpc.cpp
Expand Up @@ -69,7 +69,7 @@ void RPCTypeCheck(const Array& params,
{
string err = strprintf("Expected type %s, got %s",
Value_type_name[t], Value_type_name[v.type()]);
throw JSONRPCError(-3, err);
throw JSONRPCError(RPC_TYPE_ERROR, err);
}
i++;
}
Expand All @@ -83,13 +83,13 @@ void RPCTypeCheck(const Object& o,
{
const Value& v = find_value(o, t.first);
if (!fAllowNull && v.type() == null_type)
throw JSONRPCError(-3, strprintf("Missing %s", t.first.c_str()));
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first.c_str()));

if (!((v.type() == t.second) || (fAllowNull && (v.type() == null_type))))
{
string err = strprintf("Expected type %s for %s, got %s",
Value_type_name[t.second], t.first.c_str(), Value_type_name[v.type()]);
throw JSONRPCError(-3, err);
throw JSONRPCError(RPC_TYPE_ERROR, err);
}
}
}
Expand All @@ -98,10 +98,10 @@ int64 AmountFromValue(const Value& value)
{
double dAmount = value.get_real();
if (dAmount <= 0.0 || dAmount > 21000000.0)
throw JSONRPCError(-3, "Invalid amount");
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
int64 nAmount = roundint64(dAmount * COIN);
if (!MoneyRange(nAmount))
throw JSONRPCError(-3, "Invalid amount");
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount");
return nAmount;
}

Expand Down Expand Up @@ -317,7 +317,7 @@ string rfc1123Time()

static string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
{
if (nStatus == 401)
if (nStatus == HTTP_UNAUTHORIZED)
return strprintf("HTTP/1.0 401 Authorization Required\r\n"
"Date: %s\r\n"
"Server: bitcoin-json-rpc/%s\r\n"
Expand All @@ -335,11 +335,11 @@ static string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
"<BODY><H1>401 Unauthorized.</H1></BODY>\r\n"
"</HTML>\r\n", rfc1123Time().c_str(), FormatFullVersion().c_str());
const char *cStatus;
if (nStatus == 200) cStatus = "OK";
else if (nStatus == 400) cStatus = "Bad Request";
else if (nStatus == 403) cStatus = "Forbidden";
else if (nStatus == 404) cStatus = "Not Found";
else if (nStatus == 500) cStatus = "Internal Server Error";
if (nStatus == HTTP_OK) cStatus = "OK";
else if (nStatus == HTTP_BAD_REQUEST) cStatus = "Bad Request";
else if (nStatus == HTTP_FORBIDDEN) cStatus = "Forbidden";
else if (nStatus == HTTP_NOT_FOUND) cStatus = "Not Found";
else if (nStatus == HTTP_INTERNAL_SERVER_ERROR) cStatus = "Internal Server Error";
else cStatus = "";
return strprintf(
"HTTP/1.1 %d %s\r\n"
Expand All @@ -366,7 +366,7 @@ int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto)
vector<string> vWords;
boost::split(vWords, str, boost::is_any_of(" "));
if (vWords.size() < 2)
return 500;
return HTTP_INTERNAL_SERVER_ERROR;
proto = 0;
const char *ver = strstr(str.c_str(), "HTTP/1.");
if (ver != NULL)
Expand Down Expand Up @@ -411,7 +411,7 @@ int ReadHTTP(std::basic_istream<char>& stream, map<string, string>& mapHeadersRe
// Read header
int nLen = ReadHTTPHeader(stream, mapHeadersRet);
if (nLen < 0 || nLen > (int)MAX_SIZE)
return 500;
return HTTP_INTERNAL_SERVER_ERROR;

// Read message
if (nLen > 0)
Expand Down Expand Up @@ -484,10 +484,10 @@ string JSONRPCReply(const Value& result, const Value& error, const Value& id)
void ErrorReply(std::ostream& stream, const Object& objError, const Value& id)
{
// Send error reply from json-rpc error object
int nStatus = 500;
int nStatus = HTTP_INTERNAL_SERVER_ERROR;
int code = find_value(objError, "code").get_int();
if (code == -32600) nStatus = 400;
else if (code == -32601) nStatus = 404;
if (code == RPC_INVALID_REQUEST) nStatus = HTTP_BAD_REQUEST;
else if (code == RPC_METHOD_NOT_FOUND) nStatus = HTTP_NOT_FOUND;
string strReply = JSONRPCReply(Value::null, objError, id);
stream << HTTPReply(nStatus, strReply, false) << std::flush;
}
Expand Down Expand Up @@ -699,7 +699,7 @@ static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol,
{
// Only send a 403 if we're not using SSL to prevent a DoS during the SSL handshake.
if (!fUseSSL)
conn->stream() << HTTPReply(403, "", false) << std::flush;
conn->stream() << HTTPReply(HTTP_FORBIDDEN, "", false) << std::flush;
delete conn;
}

Expand Down Expand Up @@ -854,7 +854,7 @@ void JSONRequest::parse(const Value& valRequest)
{
// Parse request
if (valRequest.type() != obj_type)
throw JSONRPCError(-32600, "Invalid Request object");
throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
const Object& request = valRequest.get_obj();

// Parse id now so errors from here on will have the id
Expand All @@ -863,9 +863,9 @@ void JSONRequest::parse(const Value& valRequest)
// Parse method
Value valMethod = find_value(request, "method");
if (valMethod.type() == null_type)
throw JSONRPCError(-32600, "Missing method");
throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
if (valMethod.type() != str_type)
throw JSONRPCError(-32600, "Method must be a string");
throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
strMethod = valMethod.get_str();
if (strMethod != "getwork" && strMethod != "getblocktemplate")
printf("ThreadRPCServer method=%s\n", strMethod.c_str());
Expand All @@ -877,7 +877,7 @@ void JSONRequest::parse(const Value& valRequest)
else if (valParams.type() == null_type)
params = Array();
else
throw JSONRPCError(-32600, "Params must be an array");
throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array");
}

static Object JSONRPCExecOne(const Value& req)
Expand All @@ -898,7 +898,7 @@ static Object JSONRPCExecOne(const Value& req)
catch (std::exception& e)
{
rpc_result = JSONRPCReplyObj(Value::null,
JSONRPCError(-32700, e.what()), jreq.id);
JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
}

return rpc_result;
Expand Down Expand Up @@ -946,7 +946,7 @@ void ThreadRPCServer3(void* parg)
// Check authorization
if (mapHeaders.count("authorization") == 0)
{
conn->stream() << HTTPReply(401, "", false) << std::flush;
conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush;
break;
}
if (!HTTPAuthorized(mapHeaders))
Expand All @@ -958,7 +958,7 @@ void ThreadRPCServer3(void* parg)
if (mapArgs["-rpcpassword"].size() < 20)
Sleep(250);

conn->stream() << HTTPReply(401, "", false) << std::flush;
conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush;
break;
}
if (mapHeaders["connection"] == "close")
Expand All @@ -970,7 +970,7 @@ void ThreadRPCServer3(void* parg)
// Parse request
Value valRequest;
if (!read_string(strRequest, valRequest))
throw JSONRPCError(-32700, "Parse error");
throw JSONRPCError(RPC_PARSE_ERROR, "Parse error");

string strReply;

Expand All @@ -987,9 +987,9 @@ void ThreadRPCServer3(void* parg)
} else if (valRequest.type() == array_type)
strReply = JSONRPCExecBatch(valRequest.get_array());
else
throw JSONRPCError(-32700, "Top-level object parse error");
throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error");

conn->stream() << HTTPReply(200, strReply, fRun) << std::flush;
conn->stream() << HTTPReply(HTTP_OK, strReply, fRun) << std::flush;
}
catch (Object& objError)
{
Expand All @@ -998,7 +998,7 @@ void ThreadRPCServer3(void* parg)
}
catch (std::exception& e)
{
ErrorReply(conn->stream(), JSONRPCError(-32700, e.what()), jreq.id);
ErrorReply(conn->stream(), JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id);
break;
}
}
Expand All @@ -1015,13 +1015,13 @@ json_spirit::Value CRPCTable::execute(const std::string &strMethod, const json_s
// Find method
const CRPCCommand *pcmd = tableRPC[strMethod];
if (!pcmd)
throw JSONRPCError(-32601, "Method not found");
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found");

// Observe safe mode
string strWarning = GetWarnings("rpc");
if (strWarning != "" && !GetBoolArg("-disablesafemode") &&
!pcmd->okSafeMode)
throw JSONRPCError(-2, string("Safe mode: ") + strWarning);
throw JSONRPCError(RPC_FORBIDDEN_BY_SAFE_MODE, string("Safe mode: ") + strWarning);

try
{
Expand All @@ -1039,7 +1039,7 @@ json_spirit::Value CRPCTable::execute(const std::string &strMethod, const json_s
}
catch (std::exception& e)
{
throw JSONRPCError(-1, e.what());
throw JSONRPCError(RPC_MISC_ERROR, e.what());
}
}

Expand Down Expand Up @@ -1077,9 +1077,9 @@ Object CallRPC(const string& strMethod, const Array& params)
map<string, string> mapHeaders;
string strReply;
int nStatus = ReadHTTP(stream, mapHeaders, strReply);
if (nStatus == 401)
if (nStatus == HTTP_UNAUTHORIZED)
throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
else if (nStatus >= 400 && nStatus != 400 && nStatus != 404 && nStatus != 500)
else if (nStatus >= 400 && nStatus != HTTP_BAD_REQUEST && nStatus != HTTP_NOT_FOUND && nStatus != HTTP_INTERNAL_SERVER_ERROR)
throw runtime_error(strprintf("server returned HTTP error %d", nStatus));
else if (strReply.empty())
throw runtime_error("no response from server");
Expand Down
47 changes: 47 additions & 0 deletions src/bitcoinrpc.h
Expand Up @@ -18,6 +18,53 @@ class CBlockIndex;

#include "util.h"

// HTTP status codes
enum HTTPStatusCode
{
HTTP_OK = 200,
HTTP_BAD_REQUEST = 400,
HTTP_UNAUTHORIZED = 401,
HTTP_FORBIDDEN = 403,
HTTP_NOT_FOUND = 404,
HTTP_INTERNAL_SERVER_ERROR = 500,
};

// Bitcoin RPC error codes
enum RPCErrorCode
{
// Standard JSON-RPC 2.0 errors
RPC_INVALID_REQUEST = -32600,
RPC_METHOD_NOT_FOUND = -32601,
RPC_INVALID_PARAMS = -32602,
RPC_INTERNAL_ERROR = -32603,
RPC_PARSE_ERROR = -32700,

// General application defined errors
RPC_MISC_ERROR = -1, // std::exception thrown in command handling
RPC_FORBIDDEN_BY_SAFE_MODE = -2, // Server is in safe mode, and command is not allowed in safe mode
RPC_TYPE_ERROR = -3, // Unexpected type was passed as parameter
RPC_INVALID_ADDRESS_OR_KEY = -5, // Invalid address or key
RPC_OUT_OF_MEMORY = -7, // Ran out of memory during operation
RPC_INVALID_PARAMETER = -8, // Invalid, missing or duplicate parameter
RPC_DATABASE_ERROR = -20, // Database error
RPC_DESERIALIZATION_ERROR = -22, // Error parsing or validating structure in raw format

// P2P client errors
RPC_CLIENT_NOT_CONNECTED = -9, // Bitcoin is not connected
RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10, // Still downloading initial blocks

// Wallet errors
RPC_WALLET_ERROR = -4, // Unspecified problem with wallet (key not found etc.)
RPC_WALLET_INSUFFICIENT_FUNDS = -6, // Not enough funds in wallet or account
RPC_WALLET_INVALID_ACCOUNT_NAME = -11, // Invalid account name
RPC_WALLET_KEYPOOL_RAN_OUT = -12, // Keypool ran out, call keypoolrefill first
RPC_WALLET_UNLOCK_NEEDED = -13, // Enter the wallet passphrase with walletpassphrase first
RPC_WALLET_PASSPHRASE_INCORRECT = -14, // The wallet passphrase entered was incorrect
RPC_WALLET_WRONG_ENC_STATE = -15, // Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
RPC_WALLET_ENCRYPTION_FAILED = -16, // Failed to encrypt the wallet
RPC_WALLET_ALREADY_UNLOCKED = -17, // Wallet is already unlocked
};

json_spirit::Object JSONRPCError(int code, const std::string& message);

void ThreadRPCServer(void* parg);
Expand Down
2 changes: 1 addition & 1 deletion src/rpcblockchain.cpp
Expand Up @@ -150,7 +150,7 @@ Value getblock(const Array& params, bool fHelp)
uint256 hash(strHash);

if (mapBlockIndex.count(hash) == 0)
throw JSONRPCError(-5, "Block not found");
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");

CBlock block;
CBlockIndex* pblockindex = mapBlockIndex[hash];
Expand Down
10 changes: 5 additions & 5 deletions src/rpcdump.cpp
Expand Up @@ -46,7 +46,7 @@ Value importprivkey(const Array& params, bool fHelp)
CBitcoinSecret vchSecret;
bool fGood = vchSecret.SetString(strSecret);

if (!fGood) throw JSONRPCError(-5,"Invalid private key");
if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");

CKey key;
bool fCompressed;
Expand All @@ -60,7 +60,7 @@ Value importprivkey(const Array& params, bool fHelp)
pwalletMain->SetAddressBookName(vchAddress, strLabel);

if (!pwalletMain->AddKey(key))
throw JSONRPCError(-4,"Error adding key to wallet");
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");

pwalletMain->ScanForWalletTransactions(pindexGenesisBlock, true);
pwalletMain->ReacceptWalletTransactions();
Expand All @@ -79,13 +79,13 @@ Value dumpprivkey(const Array& params, bool fHelp)
string strAddress = params[0].get_str();
CBitcoinAddress address;
if (!address.SetString(strAddress))
throw JSONRPCError(-5, "Invalid Bitcoin address");
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
CKeyID keyID;
if (!address.GetKeyID(keyID))
throw JSONRPCError(-3, "Address does not refer to a key");
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
CSecret vchSecret;
bool fCompressed;
if (!pwalletMain->GetSecret(keyID, vchSecret, fCompressed))
throw JSONRPCError(-4,"Private key for address " + strAddress + " is not known");
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
return CBitcoinSecret(vchSecret, fCompressed).ToString();
}
20 changes: 10 additions & 10 deletions src/rpcmining.cpp
Expand Up @@ -96,10 +96,10 @@ Value getwork(const Array& params, bool fHelp)
"If [data] is specified, tries to solve the block and returns true if it was successful.");

if (vNodes.empty())
throw JSONRPCError(-9, "Bitcoin is not connected!");
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Bitcoin is not connected!");

if (IsInitialBlockDownload())
throw JSONRPCError(-10, "Bitcoin is downloading blocks...");
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Bitcoin is downloading blocks...");

typedef map<uint256, pair<CBlock*, CScript> > mapNewBlock_t;
static mapNewBlock_t mapNewBlock; // FIXME: thread safety
Expand Down Expand Up @@ -136,7 +136,7 @@ Value getwork(const Array& params, bool fHelp)
// Create new block
pblock = CreateNewBlock(reservekey);
if (!pblock)
throw JSONRPCError(-7, "Out of memory");
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
vNewBlock.push_back(pblock);

// Need to update only after we know CreateNewBlock succeeded
Expand Down Expand Up @@ -174,7 +174,7 @@ Value getwork(const Array& params, bool fHelp)
// Parse parameters
vector<unsigned char> vchData = ParseHex(params[0].get_str());
if (vchData.size() != 128)
throw JSONRPCError(-8, "Invalid parameter");
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
CBlock* pdata = (CBlock*)&vchData[0];

// Byte reverse
Expand Down Expand Up @@ -230,17 +230,17 @@ Value getblocktemplate(const Array& params, bool fHelp)
/* Do nothing */
}
else
throw JSONRPCError(-8, "Invalid mode");
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
}

if (strMode != "template")
throw JSONRPCError(-8, "Invalid mode");
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");

if (vNodes.empty())
throw JSONRPCError(-9, "Bitcoin is not connected!");
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Bitcoin is not connected!");

if (IsInitialBlockDownload())
throw JSONRPCError(-10, "Bitcoin is downloading blocks...");
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Bitcoin is downloading blocks...");

static CReserveKey reservekey(pwalletMain);

Expand Down Expand Up @@ -268,7 +268,7 @@ Value getblocktemplate(const Array& params, bool fHelp)
}
pblock = CreateNewBlock(reservekey);
if (!pblock)
throw JSONRPCError(-7, "Out of memory");
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");

// Need to update only after we know CreateNewBlock succeeded
pindexPrev = pindexPrevNew;
Expand Down Expand Up @@ -369,7 +369,7 @@ Value submitblock(const Array& params, bool fHelp)
ssBlock >> block;
}
catch (std::exception &e) {
throw JSONRPCError(-22, "Block decode failed");
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
}

bool fAccepted = ProcessBlock(NULL, &block);
Expand Down

0 comments on commit 39930ca

Please sign in to comment.