Skip to content

Commit

Permalink
Remove From<DecodingError> for StatusValue impl
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchen1991 committed Sep 16, 2024
1 parent c8ad6e5 commit 1912395
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 34 deletions.
13 changes: 10 additions & 3 deletions ibc-apps/ics20-transfer/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
use displaydoc::Display;
use ibc_core::channel::types::acknowledgement::StatusValue;
use ibc_core::channel::types::channel::Order;
use ibc_core::handler::types::error::HandlerError;
use ibc_core::host::types::error::{DecodingError, HostError};
use ibc_core::host::types::identifiers::{ChannelId, PortId};
use ibc_core::primitives::prelude::*;

#[derive(Display, Debug)]
pub enum TokenTransferError {
/// host error: `{0}`
Host(HostError),
Handler(HandlerError),
/// decoding error: `{0}`
Decoding(DecodingError),
/// missing destination channel `{channel_id}` on port `{port_id}`
Expand All @@ -35,16 +36,22 @@ pub enum TokenTransferError {
impl std::error::Error for TokenTransferError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self {
Self::Host(e) => Some(e),
Self::Handler(e) => Some(e),
Self::Decoding(e) => Some(e),
_ => None,
}
}
}

impl From<HandlerError> for TokenTransferError {
fn from(e: HandlerError) -> Self {
Self::Handler(e)
}
}

impl From<HostError> for TokenTransferError {
fn from(e: HostError) -> Self {
Self::Host(e)
Self::Handler(HandlerError::Host(e))
}
}

Expand Down
32 changes: 7 additions & 25 deletions ibc-apps/ics721-nft-transfer/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,8 @@ pub fn on_recv_packet_execute(
packet: &Packet,
) -> (ModuleExtras, Acknowledgement) {
let Ok(data) = serde_json::from_slice::<PacketData>(&packet.data) else {
let ack = AcknowledgementStatus::error(
DecodingError::InvalidJson {
description: "failed to deserialize packet data".to_string(),
}
.into(),
);
let ack =
AcknowledgementStatus::error(NftTransferError::FailedToDeserializePacketData.into());
return (ModuleExtras::empty(), ack.into());
};

Expand Down Expand Up @@ -344,12 +340,7 @@ mod test {
r#"{"result":"AQ=="}"#,
);
ser_json_assert_eq(
AcknowledgementStatus::error(
DecodingError::InvalidJson {
description: "failed to deserialize packet data".to_string(),
}
.into(),
),
AcknowledgementStatus::error(NftTransferError::FailedToDeserializePacketData.into()),
r#"{"error":"invalid JSON data: `failed to deserialize packet data`"}"#,
);
}
Expand All @@ -366,13 +357,9 @@ mod test {

#[test]
fn test_ack_error_to_vec() {
let ack_error: Vec<u8> = AcknowledgementStatus::error(
DecodingError::InvalidJson {
description: "failed to deserialize packet data".to_string(),
}
.into(),
)
.into();
let ack_error: Vec<u8> =
AcknowledgementStatus::error(NftTransferError::FailedToDeserializePacketData.into())
.into();

// Check that it's the same output as ibc-go
// Note: this also implicitly checks that the ack bytes are non-empty,
Expand All @@ -396,12 +383,7 @@ mod test {
);
de_json_assert_eq(
r#"{"error":"invalid JSON data: `failed to deserialize packet data`"}"#,
AcknowledgementStatus::error(
DecodingError::InvalidJson {
description: "failed to deserialize packet data".to_string(),
}
.into(),
),
AcknowledgementStatus::error(NftTransferError::FailedToDeserializePacketData.into()),
);

assert!(serde_json::from_str::<AcknowledgementStatus>(r#"{"success":"AQ=="}"#).is_err());
Expand Down
6 changes: 0 additions & 6 deletions ibc-core/ics04-channel/types/src/acknowledgement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,3 @@ impl From<AcknowledgementStatus> for Acknowledgement {
.expect("token transfer internal error: ack is never supposed to be empty")
}
}

impl From<DecodingError> for StatusValue {
fn from(e: DecodingError) -> Self {
StatusValue::new(e.to_string()).expect("error message must not be empty")
}
}

0 comments on commit 1912395

Please sign in to comment.