Skip to content

Commit a2f7dc4

Browse files
authored
Refactor error system (#255)
* Redefine Ics20Error by displaydoc * Redefine Ics02Error by displaydoc * Update client_state.rs * Redefine Ics07Error by displaydoc * Redefine Ics03Error by displaydoc * Redefine Ics04Error by displaydoc * Redefine Ics05Error by displaydoc * Redefine Ics23Error by displaydoc * Redefine Ics24Error by displaydoc * Redefine Ics26Error by displaydoc * Redefine Ics18Error by displaydoc * Redefine ParseFailure Error * Redefine HeightError * Redefine ProofError * Redefine SignerError * Redefine TimestampOverflowError, ParseTimestampError * Redefine event Error * update ics03Error * Update Ics04Error * Update Ics26Error * Remove flex-error * Update error.rs * Create 164-refactor-error-system.md * Impl std::erorr::Error for IbcError * Add #![allow(clippy::result_large_err)] * Rename Ics02Error to ClientError * Rename Ics03Error to ConnectionError * Disassembling out PacketError * Rename Ics04Error to ChannelError * Rename Ics05Error to PortError * Rename Ics23Error to CommitmentError * Rename Ics26Error to RouterError * Rename Ics18Error to RelayerError * Rename Ics20Error to TokenTransferError * Add ContextError for ValidationContext and ExecutionContext * Update RouterError * fix clippy * Update context.rs * Update lib.rs * Secondary error display output * Remove unused Error variant * Remove unused Error variant * Update 164-refactor-error-system.md
1 parent 5de13c6 commit a2f7dc4

File tree

108 files changed

+2849
-2945
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+2849
-2945
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Remove `flex-error` and remove unused error variants([#164](https://github.com/cosmos/ibc-rs/issues/164))

crates/ibc/Cargo.toml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,24 @@ all-features = true
1818

1919
[features]
2020
default = ["std"]
21-
std = ["flex-error/std", "flex-error/eyre_tracer", "ibc-proto/std", "clock"]
21+
std = [
22+
"ibc-proto/std",
23+
"ics23/std",
24+
"serde/std",
25+
"serde_json/std",
26+
"erased-serde/std",
27+
"tracing/std",
28+
"prost/std",
29+
"bytes/std",
30+
"subtle-encoding/std",
31+
"sha2/std",
32+
"displaydoc/std",
33+
"num-traits/std",
34+
"uint/std",
35+
"primitive-types/std",
36+
"clock",
37+
"tendermint/std",
38+
]
2239
clock = ["tendermint/clock", "time/std"]
2340

2441
# This feature grants access to development-time mocking libraries, such as `MockContext` or `MockHeader`.
@@ -40,7 +57,7 @@ bytes = { version = "1.2.1", default-features = false }
4057
safe-regex = { version = "0.2.5", default-features = false }
4158
subtle-encoding = { version = "0.5", default-features = false }
4259
sha2 = { version = "0.10.6", default-features = false }
43-
flex-error = { version = "0.4.4", default-features = false }
60+
displaydoc = { version = "0.2", default-features = false }
4461
num-traits = { version = "0.2.15", default-features = false }
4562
derive_more = { version = "0.99.17", default-features = false, features = ["from", "into", "display"] }
4663
uint = { version = "0.9", default-features = false }

crates/ibc/src/applications/transfer/acknowledgement.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::fmt::{Display, Error as FmtError, Formatter};
22

33
use serde::{Deserialize, Serialize};
44

5-
use super::error::Error;
5+
use super::error::TokenTransferError;
66
use crate::core::ics26_routing::context::Acknowledgement as AckTrait;
77
use crate::prelude::*;
88

@@ -36,7 +36,7 @@ impl Acknowledgement {
3636
Self::Success(ConstAckSuccess::Success)
3737
}
3838

39-
pub fn from_error(err: Error) -> Self {
39+
pub fn from_error(err: TokenTransferError) -> Self {
4040
Self::Error(format!("{}: {}", ACK_ERR_STR, err))
4141
}
4242
}

crates/ibc/src/applications/transfer/amount.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::str::FromStr;
22
use derive_more::{Display, From, Into};
33
use serde::{Deserialize, Serialize};
44

5-
use super::error::Error;
5+
use super::error::TokenTransferError;
66
use crate::bigint::U256;
77

88
/// A type for representing token transfer amounts.
@@ -22,10 +22,10 @@ impl Amount {
2222
}
2323

2424
impl FromStr for Amount {
25-
type Err = Error;
25+
type Err = TokenTransferError;
2626

2727
fn from_str(s: &str) -> Result<Self, Self::Err> {
28-
let amount = U256::from_dec_str(s).map_err(Error::invalid_amount)?;
28+
let amount = U256::from_dec_str(s).map_err(TokenTransferError::InvalidAmount)?;
2929
Ok(Self(amount))
3030
}
3131
}

crates/ibc/src/applications/transfer/coin.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
66

77
use super::amount::Amount;
88
use super::denom::{BaseDenom, PrefixedDenom};
9-
use super::error::Error;
9+
use super::error::TokenTransferError;
1010
use crate::prelude::*;
1111
use crate::serializers::serde_string;
1212

@@ -30,35 +30,39 @@ pub struct Coin<D> {
3030

3131
impl<D: FromStr> Coin<D>
3232
where
33-
D::Err: Into<Error>,
33+
D::Err: Into<TokenTransferError>,
3434
{
35-
pub fn from_string_list(coin_str: &str) -> Result<Vec<Self>, Error> {
35+
pub fn from_string_list(coin_str: &str) -> Result<Vec<Self>, TokenTransferError> {
3636
coin_str.split(',').map(FromStr::from_str).collect()
3737
}
3838
}
3939

4040
impl<D: FromStr> FromStr for Coin<D>
4141
where
42-
D::Err: Into<Error>,
42+
D::Err: Into<TokenTransferError>,
4343
{
44-
type Err = Error;
44+
type Err = TokenTransferError;
4545

4646
#[allow(clippy::assign_op_pattern)]
47-
fn from_str(coin_str: &str) -> Result<Self, Error> {
47+
fn from_str(coin_str: &str) -> Result<Self, TokenTransferError> {
4848
// Denominations can be 3 ~ 128 characters long and support letters, followed by either
4949
// a letter, a number or a separator ('/', ':', '.', '_' or '-').
5050
// Loosely copy the regex from here:
5151
// https://github.com/cosmos/cosmos-sdk/blob/v0.45.5/types/coin.go#L760-L762
5252
let matcher = regex!(br"([0-9]+)([a-zA-Z0-9/:\\._\x2d]+)");
5353

54-
let (m1, m2) = matcher
55-
.match_slices(coin_str.as_bytes())
56-
.ok_or_else(|| Error::invalid_coin(coin_str.to_string()))?;
54+
let (m1, m2) = matcher.match_slices(coin_str.as_bytes()).ok_or_else(|| {
55+
TokenTransferError::InvalidCoin {
56+
coin: coin_str.to_string(),
57+
}
58+
})?;
5759

58-
let amount = from_utf8(m1).map_err(Error::utf8_decode)?.parse()?;
60+
let amount = from_utf8(m1)
61+
.map_err(TokenTransferError::Utf8Decode)?
62+
.parse()?;
5963

6064
let denom = from_utf8(m2)
61-
.map_err(Error::utf8_decode)?
65+
.map_err(TokenTransferError::Utf8Decode)?
6266
.parse()
6367
.map_err(Into::into)?;
6468

@@ -68,9 +72,9 @@ where
6872

6973
impl<D: FromStr> TryFrom<ProtoCoin> for Coin<D>
7074
where
71-
D::Err: Into<Error>,
75+
D::Err: Into<TokenTransferError>,
7276
{
73-
type Error = Error;
77+
type Error = TokenTransferError;
7478

7579
fn try_from(proto: ProtoCoin) -> Result<Coin<D>, Self::Error> {
7680
let denom = D::from_str(&proto.denom).map_err(Into::into)?;
@@ -108,7 +112,7 @@ mod tests {
108112
use super::*;
109113

110114
#[test]
111-
fn test_parse_raw_coin() -> Result<(), Error> {
115+
fn test_parse_raw_coin() -> Result<(), TokenTransferError> {
112116
{
113117
let coin = RawCoin::from_str("123stake")?;
114118
assert_eq!(coin.denom, "stake");
@@ -137,7 +141,7 @@ mod tests {
137141
}
138142

139143
#[test]
140-
fn test_parse_raw_coin_list() -> Result<(), Error> {
144+
fn test_parse_raw_coin_list() -> Result<(), TokenTransferError> {
141145
{
142146
let coins = RawCoin::from_string_list("123stake,1a1,999den0m")?;
143147
assert_eq!(coins.len(), 3);

0 commit comments

Comments
 (0)