Repository: unionlabs/union
File: cosmwasm/ibc-union/core/src/lib.rs (Rust)

Code before:
    #[error("{} packet acknowledgement is empty", ContractErrorKind::from(self))]
    AcknowledgementIsEmpty,
    #[error(
        "{} packet acknowledgement doesn't match, found {found} but expected {expected}",
        ContractErrorKind::from(self)
    )]
    AcknowledgementMismatch { found: Bytes, expected: Bytes },
    #[error("{} the packet already exist", ContractErrorKind::from(self))]
    PacketCommitmentAlreadyExist,
    #[error(
        "caller {caller} don't have permission to migrate the client {client} with id {client_id}"
    )]
    UnauthorizedMigration {
        client_id: u32,
        caller: Addr,
        client: Addr,
    },
    #[error("cannot migrate the client {client_id} when there's no client state")]
    CannotMigrateWithNoClientState { client_id: u32 },
    #[error(
        "cannot migrate the client {client_id} when there's no consensus state at height {height}"
    )]
    CannotMigrateWithNoConsensusState { client_id: u32, height: u64 },
}

impl ContractErrorKind {
    pub fn parse_from_error_message(s: &str) -> Option<Self> {
        let (err, _) = s.split_once(' ')?;

        err.strip_prefix("IBC_UNION_ERR_")?.parse().ok()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn display() {
        assert_eq!(
            ContractErrorKind::ArithmeticOverflow,
            ContractErrorKind::parse_from_error_message(
                &ContractError::ArithmeticOverflow.to_string()
            )
            .unwrap()
        )
    }
}


Review comment (suggestion):
make sure to prefix the error messages with `ContractErrorKind::from(self)`

Code after:
    #[error("{} packet acknowledgement is empty", ContractErrorKind::from(self))]
    AcknowledgementIsEmpty,
    #[error(
        "{} packet acknowledgement doesn't match, found {found} but expected {expected}",
        ContractErrorKind::from(self)
    )]
    AcknowledgementMismatch { found: Bytes, expected: Bytes },
    #[error("{} the packet already exist", ContractErrorKind::from(self))]
    PacketCommitmentAlreadyExist,
    #[error(
        "{} caller {caller} don't have permission to migrate the client {client} with id {client_id}", ContractErrorKind::from(self)
    )]
    UnauthorizedMigration {
        client_id: u32,
        caller: Addr,
        client: Addr,
    },
    #[error(
        "{} cannot migrate the client {client_id} when there's no client state",
        ContractErrorKind::from(self)
    )]
    CannotMigrateWithNoClientState { client_id: u32 },
    #[error(
        "{} cannot migrate the client {client_id} when there's no consensus state at height {height}", ContractErrorKind::from(self)
    )]
    CannotMigrateWithNoConsensusState { client_id: u32, height: u64 },
}

impl ContractErrorKind {
    pub fn parse_from_error_message(s: &str) -> Option<Self> {
        let (err, _) = s.split_once(' ')?;

        err.strip_prefix("IBC_UNION_ERR_")?.parse().ok()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn display() {
        assert_eq!(
            ContractErrorKind::ArithmeticOverflow,
            ContractErrorKind::parse_from_error_message(
                &ContractError::ArithmeticOverflow.to_string()
            )
            .unwrap()
        )
    }
}