@@ -289,6 +289,14 @@ namespace sdk {
289
289
return script;
290
290
}
291
291
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
+
292
300
amount add_tx_output (const network_parameters& net_params, nlohmann::json& result, wally_tx_ptr& tx,
293
301
const std::string& address, amount::value_type satoshi, const std::string& asset_id)
294
302
{
@@ -336,12 +344,12 @@ namespace sdk {
336
344
const bool has_assetid = bip21_params.contains (" assetid" );
337
345
338
346
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) ;
340
348
return std::string ();
341
349
} else if (has_assetid) {
342
350
const std::string assetid_hex = bip21_params[" assetid" ];
343
351
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) ;
345
353
return std::string ();
346
354
}
347
355
addressee[" asset_id" ] = assetid_hex;
@@ -393,16 +401,16 @@ namespace sdk {
393
401
satoshi = session.convert_amount (addressee)[" satoshi" ].get <amount::value_type>();
394
402
} catch (const user_error& ex) {
395
403
// Note the error, and create a 0 satoshi output
396
- result[ " error " ] = ex.what ();
404
+ set_tx_error ( result, ex.what () );
397
405
} catch (const std::exception&) {
398
406
// 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) ;
400
408
}
401
409
402
410
// Transactions with outputs below the dust threshold (except OP_RETURN)
403
411
// are not relayed by network nodes
404
412
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) ;
406
414
}
407
415
408
416
amount::strip_non_satoshi_keys (addressee);
0 commit comments