Skip to content

Commit 4b22f11

Browse files
committed
Use set_tx_error consistently to indicate tx errors
1 parent 984db61 commit 4b22f11

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/ga_tx.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ namespace sdk {
2424
static const std::string UTXO_SEL_DEFAULT("default"); // Use the default utxo selection strategy
2525
static const std::string UTXO_SEL_MANUAL("manual"); // Use manual utxo selection
2626

27-
static void set_tx_error(nlohmann::json& result, const std::string& error)
28-
{
29-
if (json_get_value(result, "error").empty()) {
30-
result["error"] = error;
31-
}
32-
}
33-
3427
static void add_paths(ga_session& session, nlohmann::json& utxo)
3528
{
3629
const uint32_t subaccount = json_get_value(utxo, "subaccount", 0u);
@@ -426,8 +419,7 @@ namespace sdk {
426419
const bool is_liquid = net_params.is_liquid();
427420
const auto policy_asset = is_liquid ? net_params.policy_asset() : std::string("btc");
428421

429-
auto& error = result["error"];
430-
error = std::string(); // Clear any previous error
422+
result["error"] = std::string(); // Clear any previous error
431423
result["user_signed"] = false;
432424
result["server_signed"] = false;
433425
result["liquid"] = is_liquid;

src/transaction_utils.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ namespace sdk {
289289
return script;
290290
}
291291

292+
void set_tx_error(nlohmann::json& result, const std::string& error)
293+
{
294+
auto error_p = result.find("error");
295+
if (error_p == result.end() || error_p->get<std::string>().empty()) {
296+
result["error"] = error;
297+
}
298+
}
299+
292300
amount add_tx_output(const network_parameters& net_params, nlohmann::json& result, wally_tx_ptr& tx,
293301
const std::string& address, amount::value_type satoshi, const std::string& asset_id)
294302
{
@@ -336,12 +344,12 @@ namespace sdk {
336344
const bool has_assetid = bip21_params.contains("assetid");
337345

338346
if (!has_assetid && bip21_params.contains("amount")) {
339-
result["error"] = res::id_invalid_payment_request_assetid;
347+
set_tx_error(result, res::id_invalid_payment_request_assetid);
340348
return std::string();
341349
} else if (has_assetid) {
342350
const std::string assetid_hex = bip21_params["assetid"];
343351
if (!validate_hex(assetid_hex, ASSET_TAG_LEN)) {
344-
result["error"] = res::id_invalid_payment_request_assetid;
352+
set_tx_error(result, res::id_invalid_payment_request_assetid);
345353
return std::string();
346354
}
347355
addressee["asset_id"] = assetid_hex;
@@ -393,16 +401,16 @@ namespace sdk {
393401
satoshi = session.convert_amount(addressee)["satoshi"].get<amount::value_type>();
394402
} catch (const user_error& ex) {
395403
// Note the error, and create a 0 satoshi output
396-
result["error"] = ex.what();
404+
set_tx_error(result, ex.what());
397405
} catch (const std::exception&) {
398406
// Note the error, and create a 0 satoshi output
399-
result["error"] = res::id_invalid_amount;
407+
set_tx_error(result, res::id_invalid_amount);
400408
}
401409

402410
// Transactions with outputs below the dust threshold (except OP_RETURN)
403411
// are not relayed by network nodes
404412
if (!result.value("send_all", false) && satoshi.value() < session.get_dust_threshold()) {
405-
result["error"] = res::id_invalid_amount;
413+
set_tx_error(result, res::id_invalid_amount);
406414
}
407415

408416
amount::strip_non_satoshi_keys(addressee);

src/transaction_utils.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ namespace sdk {
7373
std::vector<unsigned char> scriptpubkey_from_address(
7474
const network_parameters& net_params, const std::string& address);
7575

76+
// Set the error in a transaction, if it hasn't been set already
77+
void set_tx_error(nlohmann::json& result, const std::string& error);
78+
7679
// Add an output to a tx given its address
7780
amount add_tx_output(const network_parameters& net_params, nlohmann::json& result, wally_tx_ptr& tx,
7881
const std::string& address, amount::value_type satoshi = 0, const std::string& asset_id = {});

0 commit comments

Comments
 (0)