Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor error system #255

Merged
merged 44 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
969d65a
Redefine Ics20Error by displaydoc
DaviRain-Su Nov 22, 2022
99d8205
Redefine Ics02Error by displaydoc
DaviRain-Su Nov 22, 2022
0c73f95
Merge branch 'main' into use-displaydoc-as-error-handler
DaviRain-Su Nov 22, 2022
1b2fcc3
Update client_state.rs
DaviRain-Su Nov 22, 2022
a067a5e
Redefine Ics07Error by displaydoc
DaviRain-Su Nov 23, 2022
1af9128
Redefine Ics03Error by displaydoc
DaviRain-Su Nov 23, 2022
a2e7cbe
Redefine Ics04Error by displaydoc
DaviRain-Su Nov 23, 2022
d40612f
Redefine Ics05Error by displaydoc
DaviRain-Su Nov 23, 2022
fdbe14e
Redefine Ics23Error by displaydoc
DaviRain-Su Nov 23, 2022
34dbead
Redefine Ics24Error by displaydoc
DaviRain-Su Nov 23, 2022
c68bab8
Redefine Ics26Error by displaydoc
DaviRain-Su Nov 23, 2022
fcb198a
Redefine Ics18Error by displaydoc
DaviRain-Su Nov 23, 2022
42b9788
Redefine ParseFailure Error
DaviRain-Su Nov 23, 2022
0abb309
Redefine HeightError
DaviRain-Su Nov 23, 2022
5b9375c
Redefine ProofError
DaviRain-Su Nov 23, 2022
a11d007
Redefine SignerError
DaviRain-Su Nov 23, 2022
4d62320
Redefine TimestampOverflowError, ParseTimestampError
DaviRain-Su Nov 23, 2022
5cdfca1
Redefine event Error
DaviRain-Su Nov 23, 2022
2bcd1c9
update ics03Error
DaviRain-Su Nov 23, 2022
78d45e9
Update Ics04Error
DaviRain-Su Nov 23, 2022
2ca3b77
Update Ics26Error
DaviRain-Su Nov 23, 2022
070aea4
Remove flex-error
DaviRain-Su Nov 23, 2022
a6c2ccb
Update error.rs
DaviRain-Su Nov 23, 2022
d498573
Create 164-refactor-error-system.md
DaviRain-Su Nov 23, 2022
f0bf23b
Impl std::erorr::Error for IbcError
DaviRain-Su Nov 24, 2022
c1c0b58
Add #![allow(clippy::result_large_err)]
DaviRain-Su Nov 24, 2022
0da59db
Rename Ics02Error to ClientError
DaviRain-Su Nov 24, 2022
7cf2277
Rename Ics03Error to ConnectionError
DaviRain-Su Nov 24, 2022
77bed23
Disassembling out PacketError
DaviRain-Su Nov 24, 2022
d385798
Rename Ics04Error to ChannelError
DaviRain-Su Nov 24, 2022
1d6f64e
Rename Ics05Error to PortError
DaviRain-Su Nov 24, 2022
e3d2d33
Rename Ics23Error to CommitmentError
DaviRain-Su Nov 24, 2022
0979e1f
Rename Ics26Error to RouterError
DaviRain-Su Nov 24, 2022
29e2336
Rename Ics18Error to RelayerError
DaviRain-Su Nov 24, 2022
00ef217
Rename Ics20Error to TokenTransferError
DaviRain-Su Nov 24, 2022
686a298
Add ContextError for ValidationContext and ExecutionContext
DaviRain-Su Nov 24, 2022
995d429
Update RouterError
DaviRain-Su Nov 24, 2022
d4a6c4d
fix clippy
DaviRain-Su Nov 24, 2022
8628899
Update context.rs
DaviRain-Su Nov 25, 2022
1567a06
Update lib.rs
DaviRain-Su Nov 29, 2022
8912bde
Secondary error display output
DaviRain-Su Nov 29, 2022
8f2bbe7
Remove unused Error variant
DaviRain-Su Nov 29, 2022
2eeaff7
Remove unused Error variant
DaviRain-Su Nov 30, 2022
bba5a6d
Update 164-refactor-error-system.md
DaviRain-Su Nov 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Refactor error system ([#164](https://github.com/cosmos/ibc-rs/issues/164))
plafer marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions crates/ibc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ all-features = true

[features]
default = ["std"]
std = ["flex-error/std", "flex-error/eyre_tracer", "ibc-proto/std", "clock"]
std = ["ibc-proto/std", "clock", "displaydoc/std" ]
clock = ["tendermint/clock", "time/std"]

# This feature grants access to development-time mocking libraries, such as `MockContext` or `MockHeader`.
Expand All @@ -40,7 +40,7 @@ bytes = { version = "1.2.1", default-features = false }
safe-regex = { version = "0.2.5", default-features = false }
subtle-encoding = { version = "0.5", default-features = false }
sha2 = { version = "0.10.6", default-features = false }
flex-error = { version = "0.4.4", default-features = false }
displaydoc = { version = "0.2", default-features = false }
num-traits = { version = "0.2.15", default-features = false }
derive_more = { version = "0.99.17", default-features = false, features = ["from", "into", "display"] }
uint = { version = "0.9", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/ibc/src/applications/transfer/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl FromStr for Amount {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let amount = U256::from_dec_str(s).map_err(Error::invalid_amount)?;
let amount = U256::from_dec_str(s).map_err(Error::InvalidAmount)?;
Ok(Self(amount))
}
}
Expand Down
13 changes: 8 additions & 5 deletions crates/ibc/src/applications/transfer/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ where
// https://github.com/cosmos/cosmos-sdk/blob/v0.45.5/types/coin.go#L760-L762
let matcher = regex!(br"([0-9]+)([a-zA-Z0-9/:\\._\x2d]+)");

let (m1, m2) = matcher
.match_slices(coin_str.as_bytes())
.ok_or_else(|| Error::invalid_coin(coin_str.to_string()))?;
let (m1, m2) =
matcher
.match_slices(coin_str.as_bytes())
.ok_or_else(|| Error::InvalidCoin {
coin: coin_str.to_string(),
})?;

let amount = from_utf8(m1).map_err(Error::utf8_decode)?.parse()?;
let amount = from_utf8(m1).map_err(Error::Utf8Decode)?.parse()?;

let denom = from_utf8(m2)
.map_err(Error::utf8_decode)?
.map_err(Error::Utf8Decode)?
.parse()
.map_err(Into::into)?;

Expand Down
48 changes: 32 additions & 16 deletions crates/ibc/src/applications/transfer/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,24 @@ pub fn on_chan_open_init(
version: &Version,
) -> Result<(ModuleExtras, Version), Ics20Error> {
if order != Order::Unordered {
return Err(Ics20Error::channel_not_unordered(order));
return Err(Ics20Error::ChannelNotUnordered {
expect_order: Order::Unordered,
got_order: order,
});
}
let bound_port = ctx.get_port()?;
if port_id != &bound_port {
return Err(Ics20Error::invalid_port(port_id.clone(), bound_port));
return Err(Ics20Error::InvalidPort {
port_id: port_id.clone(),
exp_port_id: bound_port,
});
}

if !version.is_empty() && version != &Version::ics20() {
return Err(Ics20Error::invalid_version(version.clone()));
return Err(Ics20Error::InvalidVersion {
expect_version: Version::ics20(),
got_version: version.clone(),
});
}

Ok((ModuleExtras::empty(), Version::ics20()))
Expand All @@ -190,12 +199,16 @@ pub fn on_chan_open_try(
counterparty_version: &Version,
) -> Result<(ModuleExtras, Version), Ics20Error> {
if order != Order::Unordered {
return Err(Ics20Error::channel_not_unordered(order));
return Err(Ics20Error::ChannelNotUnordered {
expect_order: Order::Unordered,
got_order: order,
});
}
if counterparty_version != &Version::ics20() {
return Err(Ics20Error::invalid_counterparty_version(
counterparty_version.clone(),
));
return Err(Ics20Error::InvalidCounterpartyVersion {
expect_version: Version::ics20(),
got_version: counterparty_version.clone(),
});
}

Ok((ModuleExtras::empty(), Version::ics20()))
Expand All @@ -208,9 +221,10 @@ pub fn on_chan_open_ack(
counterparty_version: &Version,
) -> Result<ModuleExtras, Ics20Error> {
if counterparty_version != &Version::ics20() {
return Err(Ics20Error::invalid_counterparty_version(
counterparty_version.clone(),
));
return Err(Ics20Error::InvalidCounterpartyVersion {
expect_version: Version::ics20(),
got_version: counterparty_version.clone(),
});
}

Ok(ModuleExtras::empty())
Expand All @@ -229,7 +243,7 @@ pub fn on_chan_close_init(
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<ModuleExtras, Ics20Error> {
Err(Ics20Error::cant_close_channel())
Err(Ics20Error::CantCloseChannel)
}

pub fn on_chan_close_confirm(
Expand All @@ -250,7 +264,7 @@ pub fn on_recv_packet<Ctx: 'static + TokenTransferContext>(
Ok(data) => data,
Err(_) => {
return OnRecvPacketAck::Failed(Box::new(Acknowledgement::Error(
Ics20Error::packet_data_deserialization().to_string(),
Ics20Error::PacketDataDeserialization.to_string(),
)));
}
};
Expand Down Expand Up @@ -279,10 +293,10 @@ pub fn on_acknowledgement_packet(
_relayer: &Signer,
) -> Result<(), Ics20Error> {
let data = serde_json::from_slice::<PacketData>(&packet.data)
.map_err(|_| Ics20Error::packet_data_deserialization())?;
.map_err(|_| Ics20Error::PacketDataDeserialization)?;

let acknowledgement = serde_json::from_slice::<Acknowledgement>(acknowledgement.as_ref())
.map_err(|_| Ics20Error::ack_deserialization())?;
.map_err(|_| Ics20Error::AckDeserialization)?;

process_ack_packet(ctx, packet, &data, &acknowledgement)?;

Expand All @@ -305,7 +319,7 @@ pub fn on_timeout_packet(
_relayer: &Signer,
) -> Result<(), Ics20Error> {
let data = serde_json::from_slice::<PacketData>(&packet.data)
.map_err(|_| Ics20Error::packet_data_deserialization())?;
.map_err(|_| Ics20Error::PacketDataDeserialization)?;

process_timeout_packet(ctx, packet, &data)?;

Expand Down Expand Up @@ -343,7 +357,9 @@ pub(crate) mod test {
output: &mut HandlerOutputBuilder<()>,
msg: MsgTransfer<PrefixedCoin>,
) -> Result<(), Error> {
send_transfer(ctx, output, msg).map_err(|e: Ics20Error| Error::app_module(e.to_string()))
send_transfer(ctx, output, msg).map_err(|e: Ics20Error| Error::AppModule {
description: e.to_string(),
})
}

fn get_defaults() -> (
Expand Down
17 changes: 11 additions & 6 deletions crates/ibc/src/applications/transfer/denom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl FromStr for BaseDenom {

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.trim().is_empty() {
Err(Error::empty_base_denom())
Err(Error::EmptyBaseDenom)
} else {
Ok(BaseDenom(s.to_owned()))
}
Expand Down Expand Up @@ -90,16 +90,21 @@ impl<'a> TryFrom<Vec<&'a str>> for TracePath {

fn try_from(v: Vec<&'a str>) -> Result<Self, Self::Error> {
if v.len() % 2 != 0 {
return Err(Error::invalid_trace_length(v.len()));
return Err(Error::InvalidTraceLength { len: v.len() });
}

let mut trace = vec![];
let id_pairs = v.chunks_exact(2).map(|paths| (paths[0], paths[1]));
for (pos, (port_id, channel_id)) in id_pairs.rev().enumerate() {
let port_id =
PortId::from_str(port_id).map_err(|e| Error::invalid_trace_port_id(pos, e))?;
let channel_id = ChannelId::from_str(channel_id)
.map_err(|e| Error::invalid_trace_channel_id(pos, e))?;
let port_id = PortId::from_str(port_id).map_err(|e| Error::InvalidTracePortId {
pos,
validation_error: e,
})?;
let channel_id =
ChannelId::from_str(channel_id).map_err(|e| Error::InvalidTraceChannelId {
pos,
validation_error: e,
})?;
trace.push(TracePrefix {
port_id,
channel_id,
Expand Down
Loading