From 2b928d6d2a6ea492adf28cd2c5d6cb948c110bd5 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 4 Oct 2021 20:49:21 +0200 Subject: [PATCH] Fix up and document macros --- packages/cw0/src/macros.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/cw0/src/macros.rs b/packages/cw0/src/macros.rs index 856c5b3a7..d4b95ff37 100644 --- a/packages/cw0/src/macros.rs +++ b/packages/cw0/src/macros.rs @@ -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) => { @@ -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 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()); } }; }