Skip to content

Commit

Permalink
Fix up and document macros
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Oct 5, 2021
1 parent 36dc097 commit 2b928d6
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/cw0/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/// Quick check for a guard. If the condition (first argument) is false,
/// then return the second argument wrapped in Err(x).
///
/// ensure!(permissions.delegate, ContractError::DelegatePerm {});
/// is the same as
/// if !permissions.delegate {
/// return Err(ContractError::DelegatePerm {});
/// }
#[macro_export]
macro_rules! ensure {
($cond:expr, $e:expr) => {
Expand All @@ -7,11 +15,20 @@ macro_rules! ensure {
};
}

/// Returns a generic error. In general, use ensure! with a specific ContractError variant,
/// but some places we don't have one. This can make quick error messages in such cases.
/// Uses .into() so that it can return StdError or any Error type with From<StdError> implemented.
///
/// ensure_generic!(id > 0, "Bad ID");
/// is the same as
/// if !(id > 0) {
/// return Err(StdError::generic_err("Bad ID").into);
/// }
#[macro_export]
macro_rules! ensure_generic {
($cond:expr, $e:expr) => {
if !($cond) {
return cosmwasm_std::StdError::generic_err($e);
return Err(cosmwasm_std::StdError::generic_err($e).into());
}
};
}

0 comments on commit 2b928d6

Please sign in to comment.