cw_storey/
std_error.rs

1/// A trait for converting *Storey* errors into [`cosmwasm_std::StdError`].
2pub trait IntoStdError {
3    /// Converts the error into a [`cosmwasm_std::StdError`] for use with CosmWasm.
4    ///
5    /// The error ends up as a [`cosmwasm_std::StdError::GenericErr`] with the error message
6    /// being the result of calling `to_string` on the error.
7    /// 
8    /// # Example
9    /// ```
10    /// use cosmwasm_std::StdError;
11    /// use storey::containers::map::key::ArrayDecodeError;
12    /// use cw_storey::IntoStdError as _;
13    ///
14    /// let error = ArrayDecodeError::InvalidLength;
15    /// assert_eq!(error.into_std_error(), StdError::generic_err(error.to_string()));
16    /// ```
17    fn into_std_error(self) -> cosmwasm_std::StdError;
18}
19
20impl<T> IntoStdError for T
21where
22    T: storey::error::StoreyError,
23{
24    fn into_std_error(self) -> cosmwasm_std::StdError {
25        cosmwasm_std::StdError::generic_err(self.to_string())
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use cosmwasm_std::StdError;
32    use storey::error::StoreyError;
33
34    use super::*;
35
36    #[derive(Debug)]
37    struct MockError {
38        msg: String,
39    }
40
41    impl std::fmt::Display for MockError {
42        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43            write!(f, "{}", self.msg)
44        }
45    }
46
47    impl StoreyError for MockError {}
48
49    #[test]
50    fn test_into_std_error() {
51        let error = MockError {
52            msg: "An error occurred".to_string(),
53        };
54        let std_error: StdError = error.into_std_error();
55        assert_eq!(std_error, StdError::generic_err("An error occurred"));
56    }
57}