1

I want to call Error messages from different contract I am calling in ink! using the '?' operator. However it gives me the following Error:

`?` couldn't convert the error to `contract::Error`
  --> lib.rs:77:107
   |
77 |   self.diff_contract.method(value_1, value_2)?;
   |                                   ^ the trait `std::convert::From<diff_contract::diff_contract::Error>` is not implemented for `contract::Error`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = note: required because of the requirements on the impl of `std::ops::FromResidual<std::result::Result<std::convert::Infallible, diff_contract::diff_contract::Error>>` for `std::result::Result<[u8; 32], contract::Error>`

I guess I have to declare that I want to use the Error handling of the other contract in my original smart contract? If so, how can I do that?

1 Answer 1

3

If you have different errors inside the function's body, you need to define the rules for converting one error into another.

For example, if diff_contract.method(value_1, value_2) returns Resutl<(), DiffError>, and your function returns Result<(), MyCurrentError>, you need to convert DiffError into MyCurrentError.

If you define MyCurrentError, you can implement the From<DiffError> trait, and the Rust will convert your error under the hood. You can check the example here.

But if MyCurrentError is defined by someone else, you can't implement foreign traits for foreign types. In that case, you need manually map one error into another.

Your Answer

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

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