Skip to content

Commit 008117d

Browse files
committed
Fix types conversion error variants
Each error variant should be for an individual field. Add new variants where one variant was used for more than one field. Rename existing variants and update error messages to match the field names.
1 parent 69bbc6b commit 008117d

12 files changed

Lines changed: 53 additions & 39 deletions

File tree

types/src/v17/blockchain/error.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,14 @@ pub enum GetBlockHeaderError {
113113
/// Conversion of hex data to bytes failed.
114114
Hex(hex::HexToBytesError),
115115
/// Consensus decoding of bytes to header failed.
116-
Consensus(encode::Error),
116+
Header(encode::Error),
117117
}
118118

119119
impl fmt::Display for GetBlockHeaderError {
120120
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121121
match *self {
122122
Self::Hex(ref e) => write_err!(f, "conversion of hex data to bytes failed"; e),
123-
Self::Consensus(ref e) =>
124-
write_err!(f, "consensus decoding of bytes to header failed"; e),
123+
Self::Header(ref e) => write_err!(f, "consensus decoding of bytes to header failed"; e),
125124
}
126125
}
127126
}
@@ -131,7 +130,7 @@ impl std::error::Error for GetBlockHeaderError {
131130
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
132131
match *self {
133132
Self::Hex(ref e) => Some(e),
134-
Self::Consensus(ref e) => Some(e),
133+
Self::Header(ref e) => Some(e),
135134
}
136135
}
137136
}

types/src/v17/blockchain/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl GetBlockHeader {
162162
use GetBlockHeaderError as E;
163163

164164
let v = Vec::from_hex(&self.0).map_err(E::Hex)?;
165-
let header = encode::deserialize::<block::Header>(&v).map_err(E::Consensus)?;
165+
let header = encode::deserialize::<block::Header>(&v).map_err(E::Header)?;
166166

167167
Ok(model::GetBlockHeader(header))
168168
}

types/src/v17/mining/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub enum BlockTemplateTransactionError {
6565
Data(consensus::encode::FromHexError),
6666
/// Conversion of the `txid` field failed.
6767
Txid(hex::HexToArrayError),
68-
/// Conversion of the `hash` field failed.
68+
/// Conversion of the `hash` field to `wtxid` failed.
6969
Hash(hex::HexToArrayError),
7070
/// Conversion of the `fee` field failed.
7171
Fee(ParseAmountError),
@@ -77,7 +77,7 @@ impl fmt::Display for BlockTemplateTransactionError {
7777
Self::Numeric(ref e) => write_err!(f, "numeric"; e),
7878
Self::Data(ref e) => write_err!(f, "conversion of the `data` field failed"; e),
7979
Self::Txid(ref e) => write_err!(f, "conversion of the `txid` field failed"; e),
80-
Self::Hash(ref e) => write_err!(f, "conversion of the `hash` field failed"; e),
80+
Self::Hash(ref e) => write_err!(f, "conversion of the `hash` field to `wtxid` failed"; e),
8181
Self::Fee(ref e) => write_err!(f, "conversion of the `fee` field failed"; e),
8282
}
8383
}

types/src/v17/raw_transactions/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::psbt::{
1616
/// Error when converting a `DecodePsbt` type into the model type.
1717
#[derive(Debug)]
1818
pub enum DecodePsbtError {
19-
/// Conversion of the `tx` field failed.
19+
/// Conversion of the `tx` field to `unsigned_tx` failed.
2020
Tx(RawTransactionError),
2121
/// Conversion of one the map items in the `unknown` field failed.
2222
Unknown(hex::HexToBytesError),
@@ -31,7 +31,7 @@ pub enum DecodePsbtError {
3131
impl fmt::Display for DecodePsbtError {
3232
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3333
match *self {
34-
Self::Tx(ref e) => write_err!(f, "conversion of raw transaction data field failed"; e),
34+
Self::Tx(ref e) => write_err!(f, "conversion of the `tx` field to `unsigned_tx` failed"; e),
3535
Self::Unknown(ref e) =>
3636
write_err!(f, "conversion of one the map items in the `unknown` field failed"; e),
3737
Self::Inputs(ref e) => write_err!(f, "conversion of one of the PSBT inputs failed"; e),

types/src/v23/raw_transactions/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::error::write_err;
1111
/// Error when converting a `DecodePsbt` type into the model type.
1212
#[derive(Debug)]
1313
pub enum DecodePsbtError {
14-
/// Conversion of the `tx` field failed.
14+
/// Conversion of the `tx` field to `unsigned_tx` failed.
1515
Tx(RawTransactionError),
1616
/// Conversion of the `global_xpubs` field failed.
1717
GlobalXpubs(GlobalXpubError),
@@ -30,7 +30,7 @@ pub enum DecodePsbtError {
3030
impl fmt::Display for DecodePsbtError {
3131
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3232
match *self {
33-
Self::Tx(ref e) => write_err!(f, "conversion of raw transaction data field failed"; e),
33+
Self::Tx(ref e) => write_err!(f, "conversion of the `tx` field to `unsigned_tx` failed"; e),
3434
Self::GlobalXpubs(ref e) =>
3535
write_err!(f, "conversion of one the map items in the `global_xbubs` field failed"; e),
3636
Self::Proprietary(ref e) =>

types/src/v24/raw_transactions/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::error::write_err;
1212
/// Error when converting a `DecodePsbt` type into the model type.
1313
#[derive(Debug)]
1414
pub enum DecodePsbtError {
15-
/// Conversion of the `tx` field failed.
15+
/// Conversion of the `tx` field to `unsigned_tx` failed.
1616
Tx(RawTransactionError),
1717
/// Conversion of the `global_xpubs` field failed.
1818
GlobalXpubs(GlobalXpubError),
@@ -31,7 +31,7 @@ pub enum DecodePsbtError {
3131
impl fmt::Display for DecodePsbtError {
3232
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3333
match *self {
34-
Self::Tx(ref e) => write_err!(f, "conversion of raw transaction data field failed"; e),
34+
Self::Tx(ref e) => write_err!(f, "conversion of the `tx` field to `unsigned_tx` failed"; e),
3535
Self::GlobalXpubs(ref e) =>
3636
write_err!(f, "conversion of one the map items in the `global_xbubs` field failed"; e),
3737
Self::Proprietary(ref e) =>

types/src/v29/blockchain/error.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,14 @@ pub enum GetBlockHeaderError {
289289
/// Conversion of hex data to bytes failed.
290290
Hex(hex::HexToBytesError),
291291
/// Consensus decoding of bytes to header failed.
292-
Consensus(encode::Error),
292+
Header(encode::Error),
293293
}
294294

295295
impl fmt::Display for GetBlockHeaderError {
296296
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
297297
match *self {
298298
Self::Hex(ref e) => write_err!(f, "conversion of hex data to bytes failed"; e),
299-
Self::Consensus(ref e) =>
300-
write_err!(f, "consensus decoding of bytes to header failed"; e),
299+
Self::Header(ref e) => write_err!(f, "consensus decoding of bytes to header failed"; e),
301300
}
302301
}
303302
}
@@ -307,7 +306,7 @@ impl std::error::Error for GetBlockHeaderError {
307306
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
308307
match *self {
309308
Self::Hex(ref e) => Some(e),
310-
Self::Consensus(ref e) => Some(e),
309+
Self::Header(ref e) => Some(e),
311310
}
312311
}
313312
}
@@ -427,8 +426,14 @@ impl From<NumericError> for GetChainStatesError {
427426
pub enum GetDescriptorActivityError {
428427
/// Conversion of numeric type (e.g., height) to expected type failed.
429428
Numeric(NumericError),
430-
/// Conversion of a hash string (BlockHash, Txid) failed.
431-
Hash(hex::HexToArrayError),
429+
/// Conversion of the `spend_txid` field failed.
430+
SpendTxid(hex::HexToArrayError),
431+
/// Conversion of the `prevout_txid` field failed.
432+
PrevoutTxid(hex::HexToArrayError),
433+
/// Conversion of the `txid` field failed.
434+
Txid(hex::HexToArrayError),
435+
/// Conversion of the `block_hash` field failed.
436+
BlockHash(hex::HexToArrayError),
432437
/// Conversion of the `amount` field (f64 BTC) failed.
433438
Amount(amount::ParseAmountError),
434439
/// Conversion of script hex to ScriptBuf failed.
@@ -449,7 +454,13 @@ impl fmt::Display for GetDescriptorActivityError {
449454
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
450455
match *self {
451456
Self::Numeric(ref e) => write_err!(f, "numeric conversion failed"; e),
452-
Self::Hash(ref e) => write_err!(f, "conversion of a hash string failed"; e),
457+
Self::SpendTxid(ref e) =>
458+
write_err!(f, "conversion of the `spend_txid` field failed"; e),
459+
Self::PrevoutTxid(ref e) =>
460+
write_err!(f, "conversion of the `prevout_txid` field failed"; e),
461+
Self::Txid(ref e) => write_err!(f, "conversion of the `txid` field failed"; e),
462+
Self::BlockHash(ref e) =>
463+
write_err!(f, "conversion of the `block_hash` field failed"; e),
453464
Self::Amount(ref e) => write_err!(f, "conversion of the `amount` field failed"; e),
454465
Self::Script(ref e) => write_err!(f, "conversion of the script `hex` field failed"; e),
455466
Self::Address(ref e) => write_err!(f, "conversion of the `address` field failed"; e),
@@ -470,7 +481,10 @@ impl std::error::Error for GetDescriptorActivityError {
470481

471482
match *self {
472483
Numeric(ref e) => Some(e),
473-
Hash(ref e) => Some(e),
484+
SpendTxid(ref e) => Some(e),
485+
PrevoutTxid(ref e) => Some(e),
486+
Txid(ref e) => Some(e),
487+
BlockHash(ref e) => Some(e),
474488
Amount(ref e) => Some(e),
475489
Script(ref e) => Some(e),
476490
Address(ref e) => Some(e),
@@ -487,7 +501,7 @@ impl From<NumericError> for GetDescriptorActivityError {
487501
}
488502

489503
impl From<hex::HexToArrayError> for GetDescriptorActivityError {
490-
fn from(e: hex::HexToArrayError) -> Self { Self::Hash(e) }
504+
fn from(e: hex::HexToArrayError) -> Self { Self::BlockHash(e) }
491505
}
492506

493507
impl From<amount::ParseAmountError> for GetDescriptorActivityError {

types/src/v29/blockchain/into.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl GetBlockHeader {
300300
use GetBlockHeaderError as E;
301301

302302
let v = Vec::from_hex(&self.0).map_err(E::Hex)?;
303-
let header = encode::deserialize::<block::Header>(&v).map_err(E::Consensus)?;
303+
let header = encode::deserialize::<block::Header>(&v).map_err(E::Header)?;
304304

305305
Ok(model::GetBlockHeader(header))
306306
}
@@ -404,11 +404,12 @@ impl GetDescriptorActivity {
404404
.block_hash
405405
.map(|s| BlockHash::from_str(&s))
406406
.transpose()
407-
.map_err(E::Hash)?;
407+
.map_err(E::BlockHash)?;
408408
let height =
409409
spend.height.map(|h| crate::to_u32(h, "height")).transpose()?;
410-
let spend_txid = Txid::from_str(&spend.spend_txid).map_err(E::Hash)?;
411-
let prevout_txid = Txid::from_str(&spend.prevout_txid).map_err(E::Hash)?;
410+
let spend_txid = Txid::from_str(&spend.spend_txid).map_err(E::SpendTxid)?;
411+
let prevout_txid =
412+
Txid::from_str(&spend.prevout_txid).map_err(E::PrevoutTxid)?;
412413
let prevout_spk = spend.prevout_spk.into_model().map_err(E::PrevoutSpk)?;
413414

414415
Ok(model::ActivityEntry::Spend(model::SpendActivity {
@@ -428,10 +429,10 @@ impl GetDescriptorActivity {
428429
.block_hash
429430
.map(|s| BlockHash::from_str(&s))
430431
.transpose()
431-
.map_err(E::Hash)?;
432+
.map_err(E::BlockHash)?;
432433
let height =
433434
receive.height.map(|h| crate::to_u32(h, "height")).transpose()?; // Uses From<NumericError>
434-
let txid = Txid::from_str(&receive.txid).map_err(E::Hash)?;
435+
let txid = Txid::from_str(&receive.txid).map_err(E::Txid)?;
435436
let output_spk = receive.output_spk.into_model().map_err(E::OutputSpk)?;
436437

437438
Ok(model::ActivityEntry::Receive(model::ReceiveActivity {

types/src/v29/hidden/error.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,20 @@ pub enum GetOrphanTxsVerboseTwoEntryError {
6666
Txid(hex::HexToArrayError),
6767
/// Conversion of the transaction `wtxid` field failed.
6868
Wtxid(hex::HexToArrayError),
69-
/// Conversion of hex data to bytes failed.
69+
/// Conversion of the `hex` field to bytes failed.
7070
Hex(hex::HexToBytesError),
71-
/// Consensus decoding of `hex` to transaction failed.
72-
Consensus(encode::Error),
71+
/// Consensus decoding of the `hex` bytes to `transaction` failed.
72+
Transaction(encode::Error),
7373
}
7474

7575
impl fmt::Display for GetOrphanTxsVerboseTwoEntryError {
7676
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7777
match *self {
7878
Self::Txid(ref e) => write_err!(f, "conversion of the `txid` field failed"; e),
7979
Self::Wtxid(ref e) => write_err!(f, "conversion of the `wtxid` field failed"; e),
80-
Self::Hex(ref e) => write_err!(f, "conversion of hex data to bytes failed"; e),
81-
Self::Consensus(ref e) =>
82-
write_err!(f, "consensus decoding of `hex` to transaction failed"; e),
80+
Self::Hex(ref e) => write_err!(f, "conversion of the `hex` field to bytes failed"; e),
81+
Self::Transaction(ref e) =>
82+
write_err!(f, "consensus decoding of the `hex` bytes to `transaction` failed"; e),
8383
}
8484
}
8585
}
@@ -91,7 +91,7 @@ impl std::error::Error for GetOrphanTxsVerboseTwoEntryError {
9191
Self::Txid(ref e) => Some(e),
9292
Self::Wtxid(ref e) => Some(e),
9393
Self::Hex(ref e) => Some(e),
94-
Self::Consensus(ref e) => Some(e),
94+
Self::Transaction(ref e) => Some(e),
9595
}
9696
}
9797
}

types/src/v29/hidden/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl GetOrphanTxsVerboseTwoEntry {
5959
let txid = self.txid.parse::<Txid>().map_err(E::Txid)?;
6060
let wtxid = self.wtxid.parse::<Wtxid>().map_err(E::Wtxid)?;
6161
let v = Vec::from_hex(&self.hex).map_err(E::Hex)?;
62-
let transaction = encode::deserialize::<Transaction>(&v).map_err(E::Consensus)?;
62+
let transaction = encode::deserialize::<Transaction>(&v).map_err(E::Transaction)?;
6363

6464
Ok(model::GetOrphanTxsVerboseTwoEntry {
6565
txid,

0 commit comments

Comments
 (0)