-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from CosmWasm/68-support-stderror
Easy conversion to `cosmwasm_std::StdError`
- Loading branch information
Showing
8 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/// A trait for converting *Storey* errors into [`cosmwasm_std::StdError`]. | ||
pub trait IntoStdError { | ||
/// Converts the error into a [`cosmwasm_std::StdError`] for use with CosmWasm. | ||
/// | ||
/// The error ends up as a [`cosmwasm_std::StdError::GenericErr`] with the error message | ||
/// being the result of calling `to_string` on the error. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// use cosmwasm_std::StdError; | ||
/// use storey::containers::map::key::ArrayDecodeError; | ||
/// use cw_storey::IntoStdError as _; | ||
/// | ||
/// let error = ArrayDecodeError::InvalidLength; | ||
/// assert_eq!(error.into_std_error(), StdError::generic_err(error.to_string())); | ||
/// ``` | ||
fn into_std_error(self) -> cosmwasm_std::StdError; | ||
} | ||
|
||
impl<T> IntoStdError for T | ||
where | ||
T: storey::error::StoreyError, | ||
{ | ||
fn into_std_error(self) -> cosmwasm_std::StdError { | ||
cosmwasm_std::StdError::generic_err(self.to_string()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use cosmwasm_std::StdError; | ||
use storey::error::StoreyError; | ||
|
||
use super::*; | ||
|
||
#[derive(Debug)] | ||
struct MockError { | ||
msg: String, | ||
} | ||
|
||
impl std::fmt::Display for MockError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.msg) | ||
} | ||
} | ||
|
||
impl StoreyError for MockError {} | ||
|
||
#[test] | ||
fn test_into_std_error() { | ||
let error = MockError { | ||
msg: "An error occurred".to_string(), | ||
}; | ||
let std_error: StdError = error.into_std_error(); | ||
assert_eq!(std_error, StdError::generic_err("An error occurred")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use std::fmt::Display; | ||
|
||
/// A trait representing a Storey error. | ||
/// | ||
/// This trait is implemented for all Storey error types, allowing third-party crates | ||
/// to implement extension traits for all of those error types. | ||
pub trait StoreyError: Display {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ | |
|
||
pub mod containers; | ||
pub mod encoding; | ||
pub mod error; | ||
pub mod storage; |