wallet: Avoid translating RPC errors when loading wallets

Common errors and warnings should be translated when displayed in the
GUI, but not translated when displayed elsewhere. The wallet method
CreateWalletFromFile does not know its caller, so this commit changes it
to return a bilingual_str to the caller.
This commit is contained in:
MarcoFalke
2019-08-19 18:12:35 -04:00
parent 608359b071
commit fae51a5c6f
18 changed files with 149 additions and 121 deletions

View File

@@ -23,6 +23,7 @@
#include <util/moneystr.h>
#include <util/string.h>
#include <util/system.h>
#include <util/translation.h>
#include <util/url.h>
#include <util/vector.h>
#include <wallet/coincontrol.h>
@@ -2589,14 +2590,14 @@ static UniValue loadwallet(const JSONRPCRequest& request)
}
}
std::string error;
std::vector<std::string> warning;
std::shared_ptr<CWallet> const wallet = LoadWallet(*g_rpc_chain, location, error, warning);
if (!wallet) throw JSONRPCError(RPC_WALLET_ERROR, error);
bilingual_str error;
std::vector<bilingual_str> warnings;
std::shared_ptr<CWallet> const wallet = LoadWallet(*g_rpc_chain, location, error, warnings);
if (!wallet) throw JSONRPCError(RPC_WALLET_ERROR, error.original);
UniValue obj(UniValue::VOBJ);
obj.pushKV("name", wallet->GetName());
obj.pushKV("warning", Join(warning, "\n"));
obj.pushKV("warning", Join(warnings, "\n", OpOriginal));
return obj;
}
@@ -2705,12 +2706,12 @@ static UniValue createwallet(const JSONRPCRequest& request)
}
SecureString passphrase;
passphrase.reserve(100);
std::vector<std::string> warnings;
std::vector<bilingual_str> warnings;
if (!request.params[3].isNull()) {
passphrase = request.params[3].get_str().c_str();
if (passphrase.empty()) {
// Empty string means unencrypted
warnings.emplace_back("Empty string given as passphrase, wallet will not be encrypted.");
warnings.emplace_back(Untranslated("Empty string given as passphrase, wallet will not be encrypted."));
}
}
@@ -2721,14 +2722,14 @@ static UniValue createwallet(const JSONRPCRequest& request)
flags |= WALLET_FLAG_DESCRIPTORS;
}
std::string error;
bilingual_str error;
std::shared_ptr<CWallet> wallet;
WalletCreationStatus status = CreateWallet(*g_rpc_chain, passphrase, flags, request.params[0].get_str(), error, warnings, wallet);
switch (status) {
case WalletCreationStatus::CREATION_FAILED:
throw JSONRPCError(RPC_WALLET_ERROR, error);
throw JSONRPCError(RPC_WALLET_ERROR, error.original);
case WalletCreationStatus::ENCRYPTION_FAILED:
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, error);
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, error.original);
case WalletCreationStatus::SUCCESS:
break;
// no default case, so the compiler can warn about missing cases
@@ -2736,7 +2737,7 @@ static UniValue createwallet(const JSONRPCRequest& request)
UniValue obj(UniValue::VOBJ);
obj.pushKV("name", wallet->GetName());
obj.pushKV("warning", Join(warnings, "\n"));
obj.pushKV("warning", Join(warnings, "\n", OpOriginal));
return obj;
}
@@ -4239,12 +4240,12 @@ static UniValue upgradewallet(const JSONRPCRequest& request)
version = request.params[0].get_int();
}
std::string error;
std::vector<std::string> warnings;
bilingual_str error;
std::vector<bilingual_str> warnings;
if (!pwallet->UpgradeWallet(version, error, warnings)) {
throw JSONRPCError(RPC_WALLET_ERROR, error);
throw JSONRPCError(RPC_WALLET_ERROR, error.original);
}
return error;
return error.original;
}
UniValue abortrescan(const JSONRPCRequest& request); // in rpcdump.cpp