Skip to main content

aranya_runtime/vm_policy/
error.rs

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