Skip to content

Commit

Permalink
wallet2: move wallet error on RPC resp handling to wallet_errors file
Browse files Browse the repository at this point in the history
Co-authored-by: jeffro256 <jeffro256@tutanota.com>
  • Loading branch information
j-berman and jeffro256 committed Jun 25, 2024
1 parent cc73fe7 commit 6c192f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
15 changes: 0 additions & 15 deletions src/wallet/wallet2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14391,21 +14391,6 @@ std::string wallet2::get_rpc_status(const std::string &s) const
return "<error>";
}
//----------------------------------------------------------------------------------------------------
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 <method>"
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
{
Expand Down
10 changes: 0 additions & 10 deletions src/wallet/wallet2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1772,7 +1763,6 @@ namespace tools
void on_device_progress(const hw::device_progress& event);

std::string get_rpc_status(const std::string &s) const;
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;
Expand Down
32 changes: 32 additions & 0 deletions src/wallet/wallet_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -1001,3 +1003,33 @@ namespace tools
LOG_ERROR(#cond << ". THROW EXCEPTION: " << #err_type); \
tools::error::throw_wallet_ex<err_type>(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 <method>"
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)

0 comments on commit 6c192f4

Please sign in to comment.