3
    #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
    #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
    pub enum MyError {
        MyError1,
        OwnableErrorCallerIsNotOwner,
        OwnableErrorNewOwnerIsZero,
    }
    impl From<OwnableError> for MyError {
        fn from(ownable: OwnableError) -> Self {
            match ownable {
                OwnableError::CallerIsNotOwner => MyError::OwnableErrorCallerIsNotOwner,
                OwnableError::NewOwnerIsZero => MyError::OwnableErrorNewOwnerIsZero,
            }
        }
    } 

2
  • pub fn function(&self) -> Result<(), MyError> {} are you talking about something like this? PSP37Error and PSP37Error Implementation
    – go11li
    Commented Nov 22, 2022 at 1:51
  • Sorry if my question is hard to understand. I'm a rust beginner. It seems inefficient to me to implement a From function to wrap all the errors of interest. So I would like to ask if there is a better way. Commented Nov 22, 2022 at 23:23

1 Answer 1

4

The best way is to have an enum field that will encapsulate the Ownable error, and implement the From trait for it:

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum MyError {
    OwnableError(OwnableError)
    MyError1,
}

impl From<OwnableError> for MyError {
    fn from(error: OwnableError) -> Self {
        MyError::OwnableError(error)
    }
}

example here

1
  • Thank you for your reply, Ossun! I think your way is better than mine. Commented Nov 24, 2022 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.