From c7e33d813d162cc50bac941751ee0802efa01e39 Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Tue, 7 Mar 2023 16:12:40 +0100 Subject: [PATCH] feat(storage): enhance error context --- contracts/cw-storage/src/contract.rs | 23 +++++++++++++++-------- contracts/cw-storage/src/error.rs | 18 +++++++++--------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/contracts/cw-storage/src/contract.rs b/contracts/cw-storage/src/contract.rs index 5d5b5d2f..38090752 100644 --- a/contracts/cw-storage/src/contract.rs +++ b/contracts/cw-storage/src/contract.rs @@ -63,21 +63,25 @@ pub mod execute { Limits { max_object_size: Some(max), .. - } if size > max => Err(BucketError::MaxObjectSizeLimitExceeded.into()), + } if size > max => Err(BucketError::MaxObjectSizeLimitExceeded(size, max).into()), Limits { max_objects: Some(max), .. - } if bucket.object_count > max => Err(BucketError::MaxObjectsLimitExceeded.into()), + } if bucket.object_count > max => { + Err(BucketError::MaxObjectsLimitExceeded(bucket.object_count, max).into()) + } Limits { max_object_pins: Some(max), .. } if pin && max < Uint128::one() => { - Err(BucketError::MaxObjectPinsLimitExceeded.into()) + Err(BucketError::MaxObjectPinsLimitExceeded(Uint128::one(), max).into()) } Limits { max_total_size: Some(max), .. - } if bucket.size > max => Err(BucketError::MaxTotalSizeLimitExceeded.into()), + } if bucket.size > max => { + Err(BucketError::MaxTotalSizeLimitExceeded(bucket.size, max).into()) + } _ => Ok(bucket), } })?; @@ -360,24 +364,27 @@ mod tests { (BucketLimits::new().set_object_pins(1u128.into()), None), ( BucketLimits::new().set_max_objects(1u128.into()), - Some(ContractError::Bucket(BucketError::MaxObjectsLimitExceeded)), + Some(ContractError::Bucket(BucketError::MaxObjectsLimitExceeded( + 2u128.into(), + 1u128.into(), + ))), ), ( BucketLimits::new().set_max_object_size(4u128.into()), Some(ContractError::Bucket( - BucketError::MaxObjectSizeLimitExceeded, + BucketError::MaxObjectSizeLimitExceeded(5u128.into(), 4u128.into()), )), ), ( BucketLimits::new().set_max_total_size(8u128.into()), Some(ContractError::Bucket( - BucketError::MaxTotalSizeLimitExceeded, + BucketError::MaxTotalSizeLimitExceeded(9u128.into(), 8u128.into()), )), ), ( BucketLimits::new().set_object_pins(0u128.into()), Some(ContractError::Bucket( - BucketError::MaxObjectPinsLimitExceeded, + BucketError::MaxObjectPinsLimitExceeded(1u128.into(), 0u128.into()), )), ), ]; diff --git a/contracts/cw-storage/src/error.rs b/contracts/cw-storage/src/error.rs index 1e03e4e3..5f82a2d1 100644 --- a/contracts/cw-storage/src/error.rs +++ b/contracts/cw-storage/src/error.rs @@ -1,4 +1,4 @@ -use cosmwasm_std::StdError; +use cosmwasm_std::{StdError, Uint128}; use thiserror::Error; #[derive(Error, Debug, PartialEq)] @@ -18,17 +18,17 @@ pub enum BucketError { #[error("Name of bucket could not be empty")] EmptyName, - #[error("Maximum total size exceeded")] - MaxTotalSizeLimitExceeded, + #[error("Maximum total size exceeded: {0} / {1}")] + MaxTotalSizeLimitExceeded(Uint128, Uint128), - #[error("Maximum objects number exceeded")] - MaxObjectsLimitExceeded, + #[error("Maximum objects number exceeded: {0} / {1}")] + MaxObjectsLimitExceeded(Uint128, Uint128), - #[error("Maximum object size exceeded")] - MaxObjectSizeLimitExceeded, + #[error("Maximum object size exceeded: {0} / {1}")] + MaxObjectSizeLimitExceeded(Uint128, Uint128), - #[error("Maximum object pins number exceeded")] - MaxObjectPinsLimitExceeded, + #[error("Maximum object pins number exceeded: {0} / {1}")] + MaxObjectPinsLimitExceeded(Uint128, Uint128), #[error("Object is already stored")] ObjectAlreadyStored,