aranya_runtime/vm_policy/
error.rs

1use alloc::{format, string::String};
2
3use crate::{engine::EngineError, storage::StorageError};
4
5#[derive(Debug, thiserror::Error)]
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    #[error("deserialize error: {0}")]
11    Deserialization(#[from] postcard::Error),
12    /// An error happened while executing policy. Stores an interior [EngineError].
13    #[error("engine error: {0}")]
14    EngineError(#[from] EngineError),
15    /// An error happened at the storage layer. Stores an interior [StorageError].
16    #[error("storage error: {0}")]
17    StorageError(#[from] StorageError),
18    /// An error happened when parsing command attributes.
19    #[error("atribute error: {0}")]
20    Attribute(#[from] AttributeError),
21    /// Some other happened and we don't know what it is.
22    #[error("unknown error")]
23    Unknown,
24}
25
26#[derive(Debug, thiserror::Error, PartialEq, Eq)]
27#[error("{0}")]
28pub struct AttributeError(pub(crate) String);
29
30impl AttributeError {
31    pub(crate) fn type_mismatch(cmd: &str, attr: &str, expected: &str, actual: &str) -> Self {
32        Self(format!("{cmd}::{attr} should be {expected}, was {actual}"))
33    }
34
35    pub(crate) fn exclusive(cmd: &str, attr1: &str, attr2: &str) -> Self {
36        Self(format!(
37            "{cmd} has both exclusive attributes {attr1} and {attr2}"
38        ))
39    }
40
41    pub(crate) fn int_range(cmd: &str, attr: &str, min: i64, max: i64) -> Self {
42        Self(format!("{cmd}::{attr} must be within [{min}, {max}]"))
43    }
44
45    pub(crate) fn missing(cmd: &str, attrs: &str) -> Self {
46        Self(format!("{cmd} is missing {attrs}"))
47    }
48}