Skip to main content

ave_core/evaluation/runner/
error.rs

1use crate::model::common::contract::ContractError;
2use thiserror::Error;
3
4#[derive(Debug, Error, Clone)]
5pub enum RunnerError {
6    #[error("[{location}] invalid event: {kind}")]
7    InvalidEvent {
8        location: &'static str,
9        kind: InvalidEventKind,
10    },
11
12    #[error("contract returned failure: {details}")]
13    ContractFailed { details: String },
14
15    #[error("contract not found: {name}")]
16    ContractNotFound { name: String },
17
18    #[error("missing helper: {name}")]
19    MissingHelper { name: &'static str },
20
21    #[error("wasm error [{operation}]: {details}")]
22    WasmError {
23        operation: &'static str,
24        details: String,
25    },
26
27    #[error("serialization error [{context}]: {details}")]
28    SerializationError {
29        context: &'static str,
30        details: String,
31    },
32
33    #[error("memory error [{operation}]: {details}")]
34    MemoryError {
35        operation: &'static str,
36        details: String,
37    },
38}
39
40#[derive(Debug, Clone)]
41pub enum InvalidEventKind {
42    Empty {
43        what: String,
44    },
45    InvalidSize {
46        field: String,
47        actual: usize,
48        max: usize,
49    },
50    ReservedWord {
51        field: String,
52        value: String,
53    },
54    NotFound {
55        what: String,
56        id: String,
57    },
58    Duplicate {
59        what: String,
60        id: String,
61    },
62    AlreadyExists {
63        what: String,
64        id: String,
65    },
66    CannotModify {
67        what: String,
68        reason: String,
69    },
70    CannotRemove {
71        what: String,
72        reason: String,
73    },
74    InvalidValue {
75        field: String,
76        reason: String,
77    },
78    SameValue {
79        what: String,
80    },
81    NotMember {
82        who: String,
83    },
84    NotSchema {
85        id: String,
86    },
87    MissingRole {
88        who: String,
89        role: String,
90        context: String,
91    },
92    InvalidQuorum {
93        context: String,
94        details: String,
95    },
96    Other {
97        msg: String,
98    },
99}
100
101impl std::fmt::Display for InvalidEventKind {
102    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103        match self {
104            Self::Empty { what } => write!(f, "{} cannot be empty", what),
105            Self::InvalidSize { field, actual, max } => {
106                write!(
107                    f,
108                    "{} size ({}) exceeds maximum ({})",
109                    field, actual, max
110                )
111            }
112            Self::ReservedWord { field, value } => {
113                write!(f, "{} cannot be '{}' (reserved word)", field, value)
114            }
115            Self::NotFound { what, id } => {
116                write!(f, "{} '{}' not found", what, id)
117            }
118            Self::Duplicate { what, id } => {
119                write!(f, "{} '{}' already exists", what, id)
120            }
121            Self::AlreadyExists { what, id } => {
122                write!(f, "{} '{}' already exists", what, id)
123            }
124            Self::CannotModify { what, reason } => {
125                write!(f, "cannot modify {}: {}", what, reason)
126            }
127            Self::CannotRemove { what, reason } => {
128                write!(f, "cannot remove {}: {}", what, reason)
129            }
130            Self::InvalidValue { field, reason } => {
131                write!(f, "invalid value for {}: {}", field, reason)
132            }
133            Self::SameValue { what } => {
134                write!(f, "{} is already set to this value", what)
135            }
136            Self::NotMember { who } => {
137                write!(f, "'{}' is not a member of governance", who)
138            }
139            Self::NotSchema { id } => write!(f, "'{}' is not a schema", id),
140            Self::MissingRole { who, role, context } => {
141                write!(
142                    f,
143                    "'{}' does not have role '{}' for {}",
144                    who, role, context
145                )
146            }
147            Self::InvalidQuorum { context, details } => {
148                write!(f, "invalid quorum for {}: {}", context, details)
149            }
150            Self::Other { msg } => write!(f, "{}", msg),
151        }
152    }
153}
154
155impl From<ContractError> for RunnerError {
156    fn from(error: ContractError) -> Self {
157        match error {
158            ContractError::MemoryAllocationFailed { .. }
159            | ContractError::InvalidPointer { .. }
160            | ContractError::WriteOutOfBounds { .. }
161            | ContractError::AllocationTooLarge { .. }
162            | ContractError::TotalMemoryExceeded { .. }
163            | ContractError::AllocationOverflow => Self::MemoryError {
164                operation: "contract memory operation",
165                details: error.to_string(),
166            },
167            ContractError::LinkerError { function, details } => {
168                Self::WasmError {
169                    operation: function,
170                    details,
171                }
172            }
173        }
174    }
175}