cosmwasm_vm/errors/
region_validation_error.rs

1use std::fmt::Debug;
2use thiserror::Error;
3
4/// An error validating a Region
5#[derive(Error, Debug)]
6#[non_exhaustive]
7pub enum RegionValidationError {
8    #[error(
9        "Region length exceeds capacity. Length {}, capacity {}",
10        length,
11        capacity
12    )]
13    LengthExceedsCapacity { length: u32, capacity: u32 },
14    #[error(
15        "Region exceeds address space. Offset {}, capacity {}",
16        offset,
17        capacity
18    )]
19    OutOfRange { offset: u32, capacity: u32 },
20    #[error("Got a zero Wasm address in the offset")]
21    ZeroOffset {},
22}
23
24impl RegionValidationError {
25    pub(crate) fn length_exceeds_capacity(length: u32, capacity: u32) -> Self {
26        RegionValidationError::LengthExceedsCapacity { length, capacity }
27    }
28
29    pub(crate) fn out_of_range(offset: u32, capacity: u32) -> Self {
30        RegionValidationError::OutOfRange { offset, capacity }
31    }
32
33    pub(crate) fn zero_offset() -> Self {
34        RegionValidationError::ZeroOffset {}
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    // constructors
43
44    #[test]
45    fn length_exceeds_capacity_works() {
46        let error = RegionValidationError::length_exceeds_capacity(50, 20);
47        match error {
48            RegionValidationError::LengthExceedsCapacity {
49                length, capacity, ..
50            } => {
51                assert_eq!(length, 50);
52                assert_eq!(capacity, 20);
53            }
54            e => panic!("Unexpected error: {e:?}"),
55        }
56    }
57
58    #[test]
59    fn out_of_range_works() {
60        let error = RegionValidationError::out_of_range(u32::MAX, 1);
61        match error {
62            RegionValidationError::OutOfRange {
63                offset, capacity, ..
64            } => {
65                assert_eq!(offset, u32::MAX);
66                assert_eq!(capacity, 1);
67            }
68            e => panic!("Unexpected error: {e:?}"),
69        }
70    }
71
72    #[test]
73    fn zero_offset() {
74        let error = RegionValidationError::zero_offset();
75        match error {
76            RegionValidationError::ZeroOffset { .. } => {}
77            e => panic!("Unexpected error: {e:?}"),
78        }
79    }
80}