1#![no_std]
22#![warn(missing_docs)]
23#![doc(html_logo_url = "https://docs.gear.rs/logo.svg")]
24#![doc(html_favicon_url = "https://gear-tech.io/favicons/favicon.ico")]
25
26extern crate alloc;
27
28mod simple;
29
30use core::fmt::Debug;
31use enum_iterator::Sequence;
32#[cfg(feature = "codec")]
33use {
34 alloc::vec::Vec,
35 scale_info::scale::{Decode, Encode, Error, Input},
36};
37
38pub use simple::*;
39
40#[derive(
42 Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Sequence, derive_more::Display,
43)]
44#[non_exhaustive]
45#[repr(u32)]
46pub enum ExecutionError {
47 #[display("Not enough gas for operation")]
49 NotEnoughGas = 100,
50
51 #[display("Not enough value for operation")]
53 NotEnoughValue = 101,
54
55 #[display("Length is overflowed to read payload")]
57 TooBigReadLen = 103,
58
59 #[display("Cannot take data in payload range from message with size")]
61 ReadWrongRange = 104,
62
63 #[display("Not running in reply context")]
65 NoReplyContext = 105,
66
67 #[display("Not running in signal context")]
69 NoSignalContext = 106,
70
71 #[display("No status code in reply/signal context")]
73 NoStatusCodeContext = 107,
74
75 #[display("Reply sending is only allowed in `init` and `handle` functions")]
77 IncorrectEntryForReply = 108,
78}
79
80#[derive(
82 Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, Sequence, derive_more::Display,
83)]
84#[non_exhaustive]
85#[repr(u32)]
86pub enum MemoryError {
87 #[display("Trying to allocate more memory in block-chain runtime than allowed")]
89 RuntimeAllocOutOfBounds = 200,
90 #[display("Trying to access memory outside wasm program memory")]
92 AccessOutOfBounds = 201,
93}
94
95#[derive(
97 Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Sequence, derive_more::Display,
98)]
99#[non_exhaustive]
100#[repr(u32)]
101pub enum MessageError {
102 #[display("Max message size exceed")]
104 MaxMessageSizeExceed = 300,
105
106 #[display("Message limit exceeded")]
110 OutgoingMessagesAmountLimitExceeded = 301,
111
112 #[display("Duplicate reply message")]
114 DuplicateReply = 302,
115
116 #[display("Duplicate waking message")]
119 DuplicateWaking = 303,
120
121 #[display("An attempt to commit or push a payload into an already formed message")]
123 LateAccess = 304,
124
125 #[display("Message with given handle is not found")]
127 OutOfBounds = 305,
128
129 #[display("Duplicated program initialization message")]
132 DuplicateInit = 306,
133
134 #[display("In case of non-zero message value must be greater than existential deposit")]
137 InsufficientValue = 307,
138
139 #[display("In case of non-zero message gas limit must be greater than mailbox threshold")]
144 InsufficientGasLimit = 308,
145
146 #[display("Reply deposit already exists for given message")]
149 DuplicateReplyDeposit = 309,
150
151 #[display(
154 "Reply deposit could be only created for init or handle message sent within the execution"
155 )]
156 IncorrectMessageForReplyDeposit = 310,
157
158 #[display("Outgoing messages bytes limit exceeded")]
161 OutgoingMessagesBytesLimitExceeded = 311,
162
163 #[display("Offset value for the input payload is out of it's size bounds")]
166 OutOfBoundsInputSliceOffset = 312,
167
168 #[display("Too big length value is set to form a slice (range) of the input buffer")]
171 OutOfBoundsInputSliceLength = 313,
172
173 #[display("Not enough gas to hold dispatch message")]
176 InsufficientGasForDelayedSending = 399,
177}
178
179#[derive(
181 Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, Sequence, derive_more::Display,
182)]
183#[non_exhaustive]
184#[repr(u32)]
185pub enum ReservationError {
187 #[display("Invalid reservation ID")]
189 InvalidReservationId = 500,
190 #[display("Reservation limit has reached")]
192 ReservationsLimitReached = 501,
193 #[display("Reservation duration cannot be zero")]
195 ZeroReservationDuration = 502,
196 #[display("Reservation amount cannot be zero")]
198 ZeroReservationAmount = 503,
199 #[display("Reservation amount cannot be below mailbox threshold")]
201 ReservationBelowMailboxThreshold = 504,
202}
203
204#[derive(
206 Debug,
207 Clone,
208 Copy,
209 Eq,
210 PartialEq,
211 Hash,
212 PartialOrd,
213 Ord,
214 Sequence,
215 derive_more::Display,
216 derive_more::From,
217)]
218#[non_exhaustive]
219pub enum ExtError {
220 #[display("Execution error: {_0}")]
222 Execution(ExecutionError),
223
224 #[display("Memory error: {_0}")]
226 Memory(MemoryError),
227
228 #[display("Message error: {_0}")]
230 Message(MessageError),
231
232 #[display("Reservation error: {_0}")]
234 Reservation(ReservationError),
235
236 Unsupported,
238}
239
240impl ExtError {
241 pub fn to_u32(self) -> u32 {
243 match self {
244 ExtError::Execution(err) => err as u32,
245 ExtError::Memory(err) => err as u32,
246 ExtError::Message(err) => err as u32,
247 ExtError::Reservation(err) => err as u32,
248 ExtError::Unsupported => u32::MAX,
249 }
250 }
251
252 pub fn from_u32(code: u32) -> Option<Self> {
254 match code {
255 100 => Some(ExecutionError::NotEnoughGas.into()),
256 101 => Some(ExecutionError::NotEnoughValue.into()),
257 103 => Some(ExecutionError::TooBigReadLen.into()),
258 104 => Some(ExecutionError::ReadWrongRange.into()),
259 105 => Some(ExecutionError::NoReplyContext.into()),
260 106 => Some(ExecutionError::NoSignalContext.into()),
261 107 => Some(ExecutionError::NoStatusCodeContext.into()),
262 108 => Some(ExecutionError::IncorrectEntryForReply.into()),
263 200 => Some(MemoryError::RuntimeAllocOutOfBounds.into()),
265 201 => Some(MemoryError::AccessOutOfBounds.into()),
266 300 => Some(MessageError::MaxMessageSizeExceed.into()),
268 301 => Some(MessageError::OutgoingMessagesAmountLimitExceeded.into()),
269 302 => Some(MessageError::DuplicateReply.into()),
270 303 => Some(MessageError::DuplicateWaking.into()),
271 304 => Some(MessageError::LateAccess.into()),
272 305 => Some(MessageError::OutOfBounds.into()),
273 306 => Some(MessageError::DuplicateInit.into()),
274 307 => Some(MessageError::InsufficientValue.into()),
275 308 => Some(MessageError::InsufficientGasLimit.into()),
276 309 => Some(MessageError::DuplicateReplyDeposit.into()),
277 310 => Some(MessageError::IncorrectMessageForReplyDeposit.into()),
278 311 => Some(MessageError::OutgoingMessagesBytesLimitExceeded.into()),
279 312 => Some(MessageError::OutOfBoundsInputSliceOffset.into()),
280 313 => Some(MessageError::OutOfBoundsInputSliceLength.into()),
281 399 => Some(MessageError::InsufficientGasForDelayedSending.into()),
282 500 => Some(ReservationError::InvalidReservationId.into()),
284 501 => Some(ReservationError::ReservationsLimitReached.into()),
285 502 => Some(ReservationError::ZeroReservationDuration.into()),
286 503 => Some(ReservationError::ZeroReservationAmount.into()),
287 504 => Some(ReservationError::ReservationBelowMailboxThreshold.into()),
288 0xffff |
290 600 |
291 u32::MAX => Some(ExtError::Unsupported),
292 _ => None,
293 }
294 }
295}
296
297#[cfg(feature = "codec")]
298impl Encode for ExtError {
299 fn encode(&self) -> Vec<u8> {
300 ExtError::to_u32(*self).to_le_bytes().to_vec()
301 }
302}
303
304#[cfg(feature = "codec")]
305impl Decode for ExtError {
306 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
307 let mut code = [0; 4];
308 input.read(&mut code)?;
309 let err =
310 ExtError::from_u32(u32::from_le_bytes(code)).ok_or("Failed to decode error code")?;
311 Ok(err)
312 }
313}
314
315#[cfg(test)]
316mod tests {
317 use super::*;
318 use alloc::collections::BTreeMap;
319
320 #[test]
321 fn error_codes_are_unique() {
322 let mut codes = BTreeMap::new();
323
324 for err in enum_iterator::all::<ExtError>() {
325 let code = err.to_u32();
326 if let Some(same_code_err) = codes.insert(code, err) {
327 panic!("{:?} has same code {:?} as {:?}", same_code_err, code, err);
328 }
329 }
330 }
331
332 #[test]
333 fn encode_decode() {
334 for err in enum_iterator::all::<ExtError>() {
335 let code = err.to_u32();
336 let decoded = ExtError::from_u32(code)
337 .unwrap_or_else(|| unreachable!("failed to decode error code: {}", code));
338 assert_eq!(err, decoded);
339 }
340 }
341
342 #[test]
343 fn error_code_no_specific_value() {
344 for err in enum_iterator::all::<ExtError>() {
345 let code = err.to_u32();
346 assert_ne!(code, 0); }
348 }
349
350 #[test]
360 fn error_codes_forbidden() {
361 let codes = [
362 0xffff, 600, ];
365
366 for code in codes {
368 let err = ExtError::from_u32(code);
369 assert_eq!(err, Some(ExtError::Unsupported));
370 }
371
372 for err in enum_iterator::all::<ExtError>() {
374 let code = err.to_u32();
375 assert!(!codes.contains(&code));
376 }
377 }
378}