aranya_runtime/vm_policy/
error.rs

1use core::fmt;
2
3use crate::{engine::EngineError, storage::StorageError};
4
5#[derive(Debug)]
6/// Errors that can occur because of creation or use of VmPolicy.
7pub enum VmPolicyError {
8    /// An error happened while deserializing a command struct. Stores an interior
9    /// [postcard::Error].
10    Deserialization(postcard::Error),
11    /// An error happened while executing policy. Stores an interior [EngineError].
12    EngineError(EngineError),
13    /// An error happened at the storage layer. Stores an interior [StorageError].
14    StorageError(StorageError),
15    /// Some other happened and we don't know what it is.
16    Unknown,
17}
18
19impl fmt::Display for VmPolicyError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Self::Deserialization(e) => write!(f, "deserialize error: {e}"),
23            Self::EngineError(e) => write!(f, "engine error: {e}"),
24            Self::StorageError(e) => write!(f, "storage error: {e}"),
25            Self::Unknown => write!(f, "unknown error"),
26        }
27    }
28}
29
30impl core::error::Error for VmPolicyError {
31    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
32        match self {
33            Self::EngineError(e) => Some(e),
34            Self::StorageError(e) => Some(e),
35            _ => None,
36        }
37    }
38}
39
40impl From<EngineError> for VmPolicyError {
41    fn from(value: EngineError) -> Self {
42        VmPolicyError::EngineError(value)
43    }
44}
45
46impl From<StorageError> for VmPolicyError {
47    fn from(value: StorageError) -> Self {
48        VmPolicyError::StorageError(value)
49    }
50}