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

Migration to 0.11: errors of shared functions accross contracts #103

Closed
maurolacy opened this issue Oct 1, 2020 · 4 comments
Closed

Migration to 0.11: errors of shared functions accross contracts #103

maurolacy opened this issue Oct 1, 2020 · 4 comments

Comments

@maurolacy
Copy link
Contributor

maurolacy commented Oct 1, 2020

Facing the following error migrating errors to the new format (i.e. to 0.11):

53 | |         HandleMsg::Freeze {} => handle_freeze(deps, env),
   | |                                 ^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `error::ContractError`, found enum `cw1_whitelist::error::ContractError`

This is because handle_freeze() is being imported from cw1-whitelist, which has its own ContractError.

Opening this issue so we can document / discuss solutions to this.

@webmaster128
Copy link
Member

Here is an example of mixed type implementations: https://github.com/CosmWasm/cosmwasm/blob/v0.11.0-alpha3/contracts/staking/src/contract.rs#L56-L69

You use ? like

HandleMsg::Freeze {} => Ok(handle_freeze(deps, env)?)

to convert Result<Data, External> to Result<Data, Internal>. For this to work you need an error converter: impl From<External> for Internal.

@maurolacy
Copy link
Contributor Author

Thanks for the quick response! I'll try this and report back.

@maurolacy
Copy link
Contributor Author

maurolacy commented Oct 1, 2020

This works, thanks.

@webmaster128
Copy link
Member

webmaster128 commented Oct 1, 2020

Still figuring out the best way to map one error into the other, but code now compiles.

At least for StdError -> custom error we now recommend the thiserror crate with something like

use cosmwasm_std::{CanonicalAddr, StdError};
use thiserror::Error;

// thiserror implements Display and ToString if you
// set the `#[error("…")]` attribute for all cases
#[derive(Error, Debug)]
pub enum MyCustomError {
    #[error("{0}")]
    // let thiserror implement From<StdError> for you
    Std(#[from] StdError),
    // this is whatever we want
    #[error("Permission denied: the sender is not the current owner")]
    NotCurrentOwner {
        expected: CanonicalAddr,
        actual: CanonicalAddr,
    },
    #[error("Messages empty. Must reflect at least one message")]
    MessagesEmpty,
}

However, I don't know how this will look like if you need multiple of those container cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants