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

Clean up generic String variants in error types #1347

Merged
merged 21 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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,2 @@
- [ibc-core] Remove `ClientError::Other` variant
([\#1346](https://github.com/cosmos/ibc-rs/issues/1346))
4 changes: 2 additions & 2 deletions ibc-apps/ics20-transfer/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<(), TokenTransferError> {
Err(TokenTransferError::UnsupportedClosedChannel)
Err(TokenTransferError::InvalidClosedChannel)

Check warning on line 136 in ibc-apps/ics20-transfer/src/module.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics20-transfer/src/module.rs#L136

Added line #L136 was not covered by tests
}

pub fn on_chan_close_init_execute(
_ctx: &mut impl TokenTransferExecutionContext,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<ModuleExtras, TokenTransferError> {
Err(TokenTransferError::UnsupportedClosedChannel)
Err(TokenTransferError::InvalidClosedChannel)

Check warning on line 144 in ibc-apps/ics20-transfer/src/module.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics20-transfer/src/module.rs#L144

Added line #L144 was not covered by tests
}

pub fn on_chan_close_confirm_validate(
Expand Down
4 changes: 1 addition & 3 deletions ibc-apps/ics20-transfer/types/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ impl FromStr for Amount {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let amount = U256::from_dec_str(s).map_err(|e| {
DecodingError::invalid_raw_data(format!(
"invalid amount that could not be parsed as a U256: {e}"
))
DecodingError::invalid_raw_data(format!("amount could not be parsed as a U256: {e}"))
})?;

Ok(Self(amount))
Expand Down
2 changes: 1 addition & 1 deletion ibc-apps/ics20-transfer/types/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
.all(|x| x.is_alphanumeric() || VALID_DENOM_CHARACTERS.contains(x))
})
.ok_or(DecodingError::invalid_raw_data(format!(
"invalid coin: {coin_str}"
"coin str: {coin_str}"
)))?;

Ok(Coin {
Expand Down
4 changes: 1 addition & 3 deletions ibc-apps/ics20-transfer/types/src/denom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ impl FromStr for TracePath {
remaining_parts
.is_none()
.then_some(trace_path)
.ok_or(DecodingError::invalid_raw_data(format!(
"invalid trace path: {s}"
)))
.ok_or(DecodingError::invalid_raw_data(format!("trace path: {s}")))
}
}

Expand Down
10 changes: 5 additions & 5 deletions ibc-apps/ics20-transfer/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use ibc_core::primitives::prelude::*;

#[derive(Display, Debug, derive_more::From)]
pub enum TokenTransferError {
/// host error: `{0}`
/// host error: {0}
Host(HostError),
/// decoding error: `{0}`
/// decoding error: {0}
Decoding(DecodingError),
/// channel error: `{0}`
/// channel error: {0}
Channel(ChannelError),
/// missing destination channel `{channel_id}` on port `{port_id}`
MissingDestinationChannel {
Expand All @@ -24,8 +24,8 @@ pub enum TokenTransferError {
MismatchedChannelOrders { expected: Order, actual: Order },
/// mismatched port IDs: expected `{expected}`, actual `{actual}`
MismatchedPortIds { expected: PortId, actual: PortId },
/// channel cannot be closed
UnsupportedClosedChannel,
/// invalid channel state: cannot be closed
InvalidClosedChannel,
/// failed to deserialize packet data
FailedToDeserializePacketData,
/// failed to deserialize acknowledgement
Expand Down
6 changes: 3 additions & 3 deletions ibc-apps/ics20-transfer/types/src/msgs/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
// Packet timeout height and packet timeout timestamp cannot both be unset.
if !timeout_height_on_b.is_set() && !timeout_timestamp_on_b.is_set() {
return Err(DecodingError::missing_raw_data(
"missing timeout height or timeout timestamp",
"msg transfer timeout height or timeout timestamp",

Check warning on line 58 in ibc-apps/ics20-transfer/types/src/msgs/transfer.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics20-transfer/types/src/msgs/transfer.rs#L58

Added line #L58 was not covered by tests
));
}

Expand All @@ -65,7 +65,7 @@
packet_data: PacketData {
token: raw_msg
.token
.ok_or(DecodingError::missing_raw_data("missing token"))?
.ok_or(DecodingError::missing_raw_data("msg transfer token"))?

Check warning on line 68 in ibc-apps/ics20-transfer/types/src/msgs/transfer.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics20-transfer/types/src/msgs/transfer.rs#L68

Added line #L68 was not covered by tests
.try_into()?,
sender: raw_msg.sender.into(),
receiver: raw_msg.receiver.into(),
Expand Down Expand Up @@ -100,7 +100,7 @@
fn try_from(raw: Any) -> Result<Self, Self::Error> {
match raw.type_url.as_str() {
TYPE_URL => Ok(MsgTransfer::decode_vec(&raw.value)?),
_ => Err(DecodingError::MismatchedTypeUrls {
_ => Err(DecodingError::MismatchedResourceName {

Check warning on line 103 in ibc-apps/ics20-transfer/types/src/msgs/transfer.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics20-transfer/types/src/msgs/transfer.rs#L103

Added line #L103 was not covered by tests
expected: TYPE_URL.to_string(),
actual: raw.type_url,
})?,
Expand Down
4 changes: 2 additions & 2 deletions ibc-apps/ics721-nft-transfer/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<(), NftTransferError> {
Err(NftTransferError::UnsupportedClosedChannel)
Err(NftTransferError::InvalidClosedChannel)

Check warning on line 135 in ibc-apps/ics721-nft-transfer/src/module.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics721-nft-transfer/src/module.rs#L135

Added line #L135 was not covered by tests
}

pub fn on_chan_close_init_execute(
_ctx: &mut impl NftTransferExecutionContext,
_port_id: &PortId,
_channel_id: &ChannelId,
) -> Result<ModuleExtras, NftTransferError> {
Err(NftTransferError::UnsupportedClosedChannel)
Err(NftTransferError::InvalidClosedChannel)

Check warning on line 143 in ibc-apps/ics721-nft-transfer/src/module.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics721-nft-transfer/src/module.rs#L143

Added line #L143 was not covered by tests
}

pub fn on_chan_close_confirm_validate(
Expand Down
4 changes: 1 addition & 3 deletions ibc-apps/ics721-nft-transfer/types/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,7 @@
fn from_str(class_uri: &str) -> Result<Self, Self::Err> {
match Uri::from_str(class_uri) {
Ok(uri) => Ok(Self(uri)),
Err(err) => Err(DecodingError::invalid_raw_data(format!(
"invalid class URI: {err}"
))),
Err(err) => Err(DecodingError::invalid_raw_data(format!("class URI: {err}"))),

Check warning on line 250 in ibc-apps/ics721-nft-transfer/types/src/class.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics721-nft-transfer/types/src/class.rs#L250

Added line #L250 was not covered by tests
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions ibc-apps/ics721-nft-transfer/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ use ibc_core::primitives::prelude::*;

#[derive(Display, Debug, From)]
pub enum NftTransferError {
/// host error: `{0}`
/// host error: {0}
Host(HostError),
/// channel error: `{0}`
/// channel error: {0}
Channel(ChannelError),
/// decoding error: `{0}`
/// decoding error: {0}
Decoding(DecodingError),
/// missing destination channel `{channel_id}` on port `{port_id}`
MissingDestinationChannel {
port_id: PortId,
channel_id: ChannelId,
},
/// empty token ID
EmptyTokenId,
/// missing token ID
MissingTokenId,
/// mismatched number of token IDs: expected `{expected}`, actual `{actual}`
MismatchedNumberOfTokenIds { expected: usize, actual: usize },
/// mismatched channel orders: expected `{expected}`, actual `{actual}`
Expand All @@ -35,8 +35,8 @@ pub enum NftTransferError {
FailedToDeserializeAck,
/// failed to parse account ID
FailedToParseAccount,
/// channel cannot be closed
UnsupportedClosedChannel,
/// invalid channel state: cannot be closed
InvalidClosedChannel,
}

#[cfg(feature = "std")]
Expand Down
7 changes: 3 additions & 4 deletions ibc-apps/ics721-nft-transfer/types/src/msgs/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use ibc_proto::ibc::applications::nft_transfer::v1::MsgTransfer as RawMsgTransfer;
use ibc_proto::Protobuf;

use crate::error::NftTransferError;
use crate::packet::PacketData;

pub(crate) const TYPE_URL: &str = "/ibc.applications.nft_transfer.v1.MsgTransfer";
Expand Down Expand Up @@ -115,12 +114,12 @@
impl Protobuf<RawMsgTransfer> for MsgTransfer {}

impl TryFrom<Any> for MsgTransfer {
type Error = NftTransferError;
type Error = DecodingError;

fn try_from(raw: Any) -> Result<Self, Self::Error> {
match raw.type_url.as_str() {
TYPE_URL => Ok(MsgTransfer::decode_vec(&raw.value).map_err(DecodingError::Protobuf)?),
_ => Err(DecodingError::MismatchedTypeUrls {
TYPE_URL => Ok(MsgTransfer::decode_vec(&raw.value)?),
_ => Err(DecodingError::MismatchedResourceName {

Check warning on line 122 in ibc-apps/ics721-nft-transfer/types/src/msgs/transfer.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics721-nft-transfer/types/src/msgs/transfer.rs#L121-L122

Added lines #L121 - L122 were not covered by tests
expected: TYPE_URL.to_string(),
actual: raw.type_url,
})?,
Expand Down
2 changes: 1 addition & 1 deletion ibc-apps/ics721-nft-transfer/types/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
/// Performs the basic validation of the packet data fields.
pub fn validate_basic(&self) -> Result<(), NftTransferError> {
if self.token_ids.0.is_empty() {
return Err(NftTransferError::EmptyTokenId);
return Err(NftTransferError::MissingTokenId);

Check warning on line 95 in ibc-apps/ics721-nft-transfer/types/src/packet.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics721-nft-transfer/types/src/packet.rs#L95

Added line #L95 was not covered by tests
}
let num = self.token_ids.0.len();
let num_uri = self
Expand Down
7 changes: 3 additions & 4 deletions ibc-clients/ics07-tendermint/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
//! Rust). As such, this module also includes some trait implementations that
//! serve to pass through traits implemented on the wrapped `ClientState` type.

use ibc_client_tendermint_types::error::TendermintClientError;
use ibc_client_tendermint_types::proto::v1::ClientState as RawTmClientState;
use ibc_client_tendermint_types::ClientState as ClientStateType;
use ibc_core_client::types::error::ClientError;
use ibc_core_host::types::error::DecodingError;
use ibc_primitives::prelude::*;
use ibc_primitives::proto::{Any, Protobuf};

Expand Down Expand Up @@ -44,7 +43,7 @@ impl ClientState {
impl Protobuf<RawTmClientState> for ClientState {}

impl TryFrom<RawTmClientState> for ClientState {
type Error = TendermintClientError;
type Error = DecodingError;

fn try_from(raw: RawTmClientState) -> Result<Self, Self::Error> {
Ok(Self(ClientStateType::try_from(raw)?))
Expand All @@ -60,7 +59,7 @@ impl From<ClientState> for RawTmClientState {
impl Protobuf<Any> for ClientState {}

impl TryFrom<Any> for ClientState {
type Error = ClientError;
type Error = DecodingError;

fn try_from(raw: Any) -> Result<Self, Self::Error> {
Ok(Self(ClientStateType::try_from(raw)?))
Expand Down
8 changes: 4 additions & 4 deletions ibc-clients/ics07-tendermint/src/client_state/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@
let tm_consensus_state = TmConsensusState::try_from(consensus_state)?;

if tm_consensus_state.root().is_empty() {
Err(CommitmentError::EmptyCommitmentRoot)?;
Err(CommitmentError::MissingCommitmentRoot)?;

Check warning on line 162 in ibc-clients/ics07-tendermint/src/client_state/common.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/ics07-tendermint/src/client_state/common.rs#L162

Added line #L162 was not covered by tests
};

if consensus_state_status(&tm_consensus_state, host_timestamp, trusting_period)?.is_expired() {
return Err(ClientError::UnexpectedStatus(Status::Expired));
return Err(ClientError::InvalidStatus(Status::Expired));
}

Ok(())
Expand Down Expand Up @@ -206,7 +206,7 @@
let latest_height = client_state.latest_height;

if latest_height < proof_height {
return Err(ClientError::InvalidProofHeight {
return Err(ClientError::InsufficientProofHeight {
actual: latest_height,
expected: proof_height,
});
Expand Down Expand Up @@ -294,7 +294,7 @@
value: Vec<u8>,
) -> Result<(), ClientError> {
if prefix.is_empty() {
Err(CommitmentError::EmptyCommitmentPrefix)?;
Err(CommitmentError::MissingCommitmentPrefix)?;

Check warning on line 297 in ibc-clients/ics07-tendermint/src/client_state/common.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/ics07-tendermint/src/client_state/common.rs#L297

Added line #L297 was not covered by tests
}

let merkle_path = MerklePath::new(vec![prefix.as_bytes().to_vec().into(), path]);
Expand Down
7 changes: 2 additions & 5 deletions ibc-clients/ics07-tendermint/src/client_state/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ibc_core_host::types::identifiers::ClientId;
use ibc_core_host::types::path::{ClientConsensusStatePath, ClientStatePath};
use ibc_primitives::prelude::*;
use ibc_primitives::proto::Any;
use ibc_primitives::TimestampError;

use super::ClientState;

Expand Down Expand Up @@ -341,11 +342,7 @@ where
let tm_consensus_state_timestamp = tm_consensus_state.timestamp();
let tm_consensus_state_expiry = (tm_consensus_state_timestamp
+ client_state.trusting_period)
.map_err(|_| ClientError::Other {
description: String::from(
"Timestamp overflow error occurred while attempting to parse TmConsensusState",
),
})?;
.map_err(|_| TimestampError::OverflowedTimestamp)?;

if tm_consensus_state_expiry > host_timestamp {
break;
Expand Down
14 changes: 8 additions & 6 deletions ibc-clients/ics07-tendermint/src/client_state/misbehaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
};
use ibc_core_client::context::{Convertible, ExtClientValidationContext};
use ibc_core_client::types::error::ClientError;
use ibc_core_host::types::error::IdentifierError;
use ibc_core_host::types::identifiers::{ChainId, ClientId};
use ibc_core_host::types::path::ClientConsensusStatePath;
use ibc_primitives::prelude::*;
Expand Down Expand Up @@ -116,12 +117,13 @@
// main header verification, delegated to the tendermint-light-client crate.
let untrusted_state = header.as_untrusted_block_state();

let tm_chain_id = &chain_id
.as_str()
.try_into()
.map_err(|e| ClientError::Other {
description: format!("failed to parse chain id: {e}"),
})?;
let tm_chain_id =
&chain_id
.as_str()
.try_into()
.map_err(|e| IdentifierError::FailedToParse {
seanchen1991 marked this conversation as resolved.
Show resolved Hide resolved
description: format!("chain ID `{chain_id}`: {e:?}"),

Check warning on line 125 in ibc-clients/ics07-tendermint/src/client_state/misbehaviour.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/ics07-tendermint/src/client_state/misbehaviour.rs#L125

Added line #L125 was not covered by tests
})?;

let trusted_state =
header.as_trusted_block_state(tm_chain_id, trusted_time, trusted_next_validator_hash)?;
Expand Down
14 changes: 7 additions & 7 deletions ibc-clients/ics07-tendermint/src/client_state/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use ibc_core_client::context::{Convertible, ExtClientValidationContext};
use ibc_core_client::types::error::ClientError;
use ibc_core_client::types::Height;
use ibc_core_host::types::error::IdentifierError;
use ibc_core_host::types::identifiers::{ChainId, ClientId};
use ibc_core_host::types::path::ClientConsensusStatePath;
use ibc_primitives::prelude::*;
Expand Down Expand Up @@ -52,18 +53,17 @@
)?;

TrustedBlockState {
chain_id: &chain_id
.as_str()
.try_into()
.map_err(|e| ClientError::Other {
description: format!("failed to parse chain id: {}", e),
})?,
chain_id: &chain_id.as_str().try_into().map_err(|e| {
IdentifierError::FailedToParse {
description: format!("chain ID `{chain_id}`: {e:?}"),
}

Check warning on line 59 in ibc-clients/ics07-tendermint/src/client_state/update_client.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/ics07-tendermint/src/client_state/update_client.rs#L57-L59

Added lines #L57 - L59 were not covered by tests
})?,
header_time: trusted_consensus_state.timestamp(),
height: header
.trusted_height
.revision_height()
.try_into()
.map_err(|_| ClientError::FailedHeaderVerification {
.map_err(|_| ClientError::FailedToVerifyHeader {
description: TendermintClientError::InvalidHeaderHeight(
header.trusted_height.revision_height(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,5 @@
&& subject_proof_specs == &substitute_proof_specs
&& subject_upgrade_path == &substitute_upgrade_path)
.then_some(())
.ok_or(ClientError::MismatchedClientRecoveryStates)
.ok_or(ClientError::FailedToVerifyClientRecoveryStates)

Check warning on line 281 in ibc-clients/ics07-tendermint/src/client_state/validation.rs

View check run for this annotation

Codecov / codecov/patch

ibc-clients/ics07-tendermint/src/client_state/validation.rs#L281

Added line #L281 was not covered by tests
}
7 changes: 3 additions & 4 deletions ibc-clients/ics07-tendermint/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
//! implementations that serve to pass through traits implemented on the wrapped
//! `ConsensusState` type.

use ibc_client_tendermint_types::error::TendermintClientError;
use ibc_client_tendermint_types::proto::v1::ConsensusState as RawTmConsensusState;
use ibc_client_tendermint_types::ConsensusState as ConsensusStateType;
use ibc_core_client::context::consensus_state::ConsensusState as ConsensusStateTrait;
use ibc_core_client::types::error::ClientError;
use ibc_core_commitment_types::commitment::CommitmentRoot;
use ibc_core_host::types::error::DecodingError;
use ibc_primitives::prelude::*;
use ibc_primitives::proto::{Any, Protobuf};
use ibc_primitives::Timestamp;
Expand Down Expand Up @@ -52,7 +51,7 @@ impl From<ConsensusState> for ConsensusStateType {
impl Protobuf<RawTmConsensusState> for ConsensusState {}

impl TryFrom<RawTmConsensusState> for ConsensusState {
type Error = TendermintClientError;
type Error = DecodingError;

fn try_from(raw: RawTmConsensusState) -> Result<Self, Self::Error> {
Ok(Self(ConsensusStateType::try_from(raw)?))
Expand All @@ -68,7 +67,7 @@ impl From<ConsensusState> for RawTmConsensusState {
impl Protobuf<Any> for ConsensusState {}

impl TryFrom<Any> for ConsensusState {
type Error = ClientError;
type Error = DecodingError;

fn try_from(raw: Any) -> Result<Self, Self::Error> {
Ok(Self(ConsensusStateType::try_from(raw)?))
Expand Down
Loading
Loading