From e866a192dd3904071b43ea2ff6d92ed0d1d8e0a6 Mon Sep 17 00:00:00 2001 From: j-berman Date: Mon, 24 Jun 2024 13:29:37 -0700 Subject: [PATCH] wallet2: move wallet error on RPC resp handling to wallet_errors file Co-authored-by: jeffro256 --- src/wallet/wallet2.cpp | 16 ---------------- src/wallet/wallet2.h | 11 ----------- src/wallet/wallet_errors.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 3874ce850..f44cfde91 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -15328,22 +15328,6 @@ void wallet2::on_device_progress(const hw::device_progress& event) m_callback->on_device_progress(event); } //---------------------------------------------------------------------------------------------------- -void wallet2::throw_on_rpc_response_error(bool r, const epee::json_rpc::error &error, const std::string &status, const char *method) const -{ - // Treat all RPC payment access errors the same, whether payment is actually required or not - THROW_WALLET_EXCEPTION_IF(error.code == CORE_RPC_ERROR_CODE_INVALID_CLIENT, tools::error::deprecated_rpc_access, method); - THROW_WALLET_EXCEPTION_IF(error.code, tools::error::wallet_coded_rpc_error, method, error.code, get_rpc_server_error_message(error.code)); - THROW_WALLET_EXCEPTION_IF(!r, tools::error::no_connection_to_daemon, method); - // empty string -> not connection - THROW_WALLET_EXCEPTION_IF(status.empty(), tools::error::no_connection_to_daemon, method); - - THROW_WALLET_EXCEPTION_IF(status == CORE_RPC_STATUS_BUSY, tools::error::daemon_busy, method); - THROW_WALLET_EXCEPTION_IF(status == CORE_RPC_STATUS_PAYMENT_REQUIRED, tools::error::deprecated_rpc_access, method); - // Deprecated RPC payment access endpoints would set status to "Client signature does not verify for " - THROW_WALLET_EXCEPTION_IF(status.compare(0, 16, "Client signature") == 0, tools::error::deprecated_rpc_access, method); -} -//---------------------------------------------------------------------------------------------------- - bool wallet2::save_to_file(const std::string& path_to_file, const std::string& raw, bool is_printable) const { if (is_printable || m_export_format == ExportFormat::Binary) diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 7228f1907..59f279cee 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -75,15 +75,6 @@ #undef MONERO_DEFAULT_LOG_CATEGORY #define MONERO_DEFAULT_LOG_CATEGORY "wallet.wallet2" -#define THROW_ON_RPC_RESPONSE_ERROR(r, error, res, method, ...) \ - do { \ - throw_on_rpc_response_error(r, error, res.status, method); \ - THROW_WALLET_EXCEPTION_IF(res.status != CORE_RPC_STATUS_OK, ## __VA_ARGS__); \ - } while(0) - -#define THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, err, res, method) \ - THROW_ON_RPC_RESPONSE_ERROR(r, err, res, method, tools::error::wallet_generic_rpc_error, method, res.status) - class Serialization_portability_wallet_Test; class wallet_accessor_test; namespace multisig { class multisig_account; } @@ -1911,8 +1902,6 @@ private: boost::optional on_device_passphrase_request(bool & on_device); void on_device_progress(const hw::device_progress& event); - void throw_on_rpc_response_error(bool r, const epee::json_rpc::error &error, const std::string &status, const char *method) const; - bool should_expand(const cryptonote::subaddress_index &index) const; bool spends_one_of_ours(const cryptonote::transaction &tx) const; diff --git a/src/wallet/wallet_errors.h b/src/wallet/wallet_errors.h index 4bbf05008..01e0ce0d1 100644 --- a/src/wallet/wallet_errors.h +++ b/src/wallet/wallet_errors.h @@ -37,7 +37,9 @@ #include "cryptonote_basic/cryptonote_format_utils.h" #include "cryptonote_core/cryptonote_tx_utils.h" +#include "net/jsonrpc_structs.h" #include "rpc/core_rpc_server_commands_defs.h" +#include "rpc/core_rpc_server_error_codes.h" #include "include_base_utils.h" @@ -1040,3 +1042,33 @@ namespace tools LOG_ERROR(#cond << ". THROW EXCEPTION: " << #err_type); \ tools::error::throw_wallet_ex(std::string(__FILE__ ":" STRINGIZE(__LINE__)), ## __VA_ARGS__); \ } + +namespace tools +{ + namespace error + { + inline void throw_on_rpc_response_error(bool r, const epee::json_rpc::error &error, const std::string &status, const char *method) + { + // Treat all RPC payment access errors the same, whether payment is actually required or not + THROW_WALLET_EXCEPTION_IF(error.code == CORE_RPC_ERROR_CODE_INVALID_CLIENT, tools::error::deprecated_rpc_access, method); + THROW_WALLET_EXCEPTION_IF(error.code, tools::error::wallet_coded_rpc_error, method, error.code, get_rpc_server_error_message(error.code)); + THROW_WALLET_EXCEPTION_IF(!r, tools::error::no_connection_to_daemon, method); + // empty string -> not connection + THROW_WALLET_EXCEPTION_IF(status.empty(), tools::error::no_connection_to_daemon, method); + + THROW_WALLET_EXCEPTION_IF(status == CORE_RPC_STATUS_BUSY, tools::error::daemon_busy, method); + THROW_WALLET_EXCEPTION_IF(status == CORE_RPC_STATUS_PAYMENT_REQUIRED, tools::error::deprecated_rpc_access, method); + // Deprecated RPC payment access endpoints would set status to "Client signature does not verify for " + THROW_WALLET_EXCEPTION_IF(status.compare(0, 16, "Client signature") == 0, tools::error::deprecated_rpc_access, method); + } + } +} + +#define THROW_ON_RPC_RESPONSE_ERROR(r, err, res, method, ...) \ + do { \ + tools::error::throw_on_rpc_response_error(r, err, res.status, method); \ + THROW_WALLET_EXCEPTION_IF(res.status != CORE_RPC_STATUS_OK, ## __VA_ARGS__); \ + } while(0) + +#define THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, err, res, method) \ + THROW_ON_RPC_RESPONSE_ERROR(r, err, res, method, tools::error::wallet_generic_rpc_error, method, res.status)