Skip to content

Commit

Permalink
feat(storage): enhance error context
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 7, 2023
1 parent 73d323d commit c7e33d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
23 changes: 15 additions & 8 deletions contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
})?;
Expand Down Expand Up @@ -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()),
)),
),
];
Expand Down
18 changes: 9 additions & 9 deletions contracts/cw-storage/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::StdError;
use cosmwasm_std::{StdError, Uint128};
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
Expand All @@ -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,
Expand Down

0 comments on commit c7e33d8

Please sign in to comment.