Open webmaster128 opened 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,
...
}
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.
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.
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.