Skip to main content

borderless_runtime/
error.rs

1use borderless::{AgentId, ContractId};
2use thiserror::Error;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, Error)]
7#[error(transparent)]
8pub struct Error {
9    kind: ErrorKind,
10}
11
12impl Error {
13    pub fn msg(msg: impl AsRef<str>) -> Self {
14        ErrorKind::Msg(msg.as_ref().to_string()).into()
15    }
16}
17
18impl From<ErrorKind> for Error {
19    fn from(value: ErrorKind) -> Self {
20        Error { kind: value }
21    }
22}
23
24impl From<borderless_kv_store::Error> for Error {
25    fn from(value: borderless_kv_store::Error) -> Self {
26        ErrorKind::from(value).into()
27    }
28}
29
30impl From<postcard::Error> for Error {
31    fn from(value: postcard::Error) -> Self {
32        ErrorKind::from(value).into()
33    }
34}
35
36impl From<serde_json::Error> for Error {
37    fn from(value: serde_json::Error) -> Self {
38        ErrorKind::from(value).into()
39    }
40}
41
42impl From<wasmtime::Error> for Error {
43    fn from(value: wasmtime::Error) -> Self {
44        ErrorKind::from(value).into()
45    }
46}
47
48#[derive(Debug, Error)]
49#[allow(dead_code)]
50pub(crate) enum ErrorKind {
51    // --- High-level errors
52    /// Storage errors
53    #[error("storage error - {0}")]
54    Db(#[from] borderless_kv_store::Error),
55
56    /// Binary-Encoding (postcard) related errors
57    #[error("encoding error (binary) - {0}")]
58    BinaryEncoding(#[from] postcard::Error),
59
60    /// Json-Encoding related errors
61    #[error("encoding error (json) - {0}")]
62    JsonEncoding(#[from] serde_json::Error),
63
64    /// Wasmtime related errors
65    #[error("wasmtime error - {0}")]
66    Wasm(#[from] wasmtime::Error),
67
68    // --- Module related errors
69    /// Module function has an incorrect type
70    #[error("exported function '{func}' has invalid type")]
71    InvalidFuncType { func: &'static str },
72
73    /// Module export is not a function
74    #[error("exported item '{func}' must be a function")]
75    InvalidExport { func: &'static str },
76
77    /// Module is missing a required export
78    #[error("module is missing required export '{func}'")]
79    MissingExport { func: &'static str },
80
81    /// Contract has not been instantiated and was not found in contract storage
82    // --- Runtime errors
83    #[error("contract is not instantiated cid={cid}")]
84    MissingContract { cid: ContractId },
85
86    #[error("contract is not instantiated aid={aid}")]
87    MissingAgent { aid: AgentId },
88
89    #[error("contract is revoked and cannot process transactions cid={cid}")]
90    RevokedContract { cid: ContractId },
91
92    #[error("agent is revoked and cannot process transactions aid={aid}")]
93    RevokedAgent { aid: AgentId },
94
95    #[error("introduction id is not for the correct type of entity - agent vs contract mismatch")]
96    InvalidIdType,
97
98    #[error("the entity was already introduced")]
99    DoubleIntroduction,
100
101    /// Missing required value in register
102    // --- Register errors
103    #[error("missing required value '{0}' in register")]
104    MissingRegisterValue(&'static str),
105
106    /// The value read from a register could not be parsed into the expected type
107    #[error("invalid value in register '{register}' - expected {expected_type}")]
108    InvalidRegisterValue {
109        register: &'static str,
110        expected_type: &'static str,
111    },
112
113    // --- VmState errors
114    #[error("running entity in VmState is marked 'immutable' - cannot mutate state or storage")]
115    Immutable,
116
117    #[error("no active entity in VmState")]
118    NoActiveEntity,
119
120    #[error("VmState is not initialized with async support")]
121    NoAsync,
122
123    /// Generic error message - useful for communicating more complicated errors
124    #[error("{0}")]
125    Msg(String),
126}