CosmWasm / cosmwasm

Framework for building smart contracts in Wasm for the Cosmos SDK
https://www.cosmwasm.com/
Apache License 2.0
1.07k stars 336 forks source link

Incomplete conversion support for StdError #2076

Open webmaster128 opened 7 months ago

webmaster128 commented 7 months ago

In the beginning we created conversion support for pretty much everything to StdError. But this also means that StdError need to centrally know about everything, which is kindof ugly.

What we do have more or less is

Did I miss something?

At this point I am not sure if and how we should implement conversions for all of those to StdError.

aumetra commented 7 months ago

Potentially add a From<ErrorType> which just converts it into a generic error for now.
Since the types stay the same and the messages should be treated as opaque anyway, we could change what branch we convert to.
If users really need to inspect the exact error, we can add those cases later.

The implementation could be made in a little declarative macro:

macro_rules! into_generic_err {
    ($($err:ty),+$(,)?) => {
        $(
            impl From<$err> for StdError {
                fn from(err: $err) -> Self {
                   // Should work because every error implements `Display` and therefore `ToString`
                   Self::generic_error(err.to_string())
                }
            }
        )+
    };
}

into_generic_err! {
    CheckedFromRatioError,
    DivisionError,
    ...
}
webmaster128 commented 7 months ago

Part of my uncertainty comes from disliking the concept of "generic error". It is used for a lot of use generated errors but also as a fallback for errors in cosmwasm-std. The "Generic error: " prefix to error messages is not helping.

    /// Whenever there is no specific error type available
    #[error("Generic error: {msg}")]
    GenericErr { msg: String, backtrace: BT },

Maybe (following your proposal) it would be better to add a StdError::Opaque which takes and error String and maybe the original type name.

webmaster128 commented 3 months ago

Let's address this as part of https://github.com/CosmWasm/cosmwasm/issues/2136. The low level error types can live in cosmwasm-core or similar places and StdError in cosmwasm-std can then be created from those.