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

Incomplete conversion support for StdError #2076

Open
8 of 16 tasks
webmaster128 opened this issue Mar 27, 2024 · 2 comments
Open
8 of 16 tasks

Incomplete conversion support for StdError #2076

webmaster128 opened this issue Mar 27, 2024 · 2 comments

Comments

@webmaster128
Copy link
Member

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

  • impl From<core::str::Utf8Error> for StdError
  • impl From<alloc::string::FromUtf8Error> for StdError
  • impl From<VerificationError> for StdError
  • impl From<RecoverPubkeyError> for StdError
  • impl From<OverflowError> for StdError
  • impl From<DivideByZeroError> for StdError
  • impl From<CoinsError> for StdError
  • impl From<CoinFromStrError> for StdError
  • CheckedFromRatioError
  • CheckedMultiplyFractionError
  • CheckedMultiplyRatioError
  • DivisionError
  • RoundUpOverflowError
  • RoundDownOverflowError
  • ConversionOverflowError
  • DecimalRangeExceeded/Decimal256RangeExceeded/SignedDecimalRangeExceeded/SignedDecimal256RangeExceeded

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
Copy link
Member

aumetra commented Mar 27, 2024

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
Copy link
Member Author

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.

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