Skip to main content

smg_wasm/
errors.rs

1//! WASM Error Types
2//!
3//! Defines comprehensive error types for the WASM subsystem,
4//! including module, manager, and runtime errors.
5
6use std::fmt;
7
8use thiserror::Error;
9
10pub type Result<T> = std::result::Result<T, WasmError>;
11
12/// SHA256 hash wrapper for display purposes
13#[derive(Debug, Clone, Copy)]
14pub struct Sha256Hash(pub [u8; 32]);
15
16impl fmt::Display for Sha256Hash {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        let hex_string: String = self.0.iter().map(|b| format!("{b:02x}")).collect();
19        write!(f, "{hex_string}")
20    }
21}
22
23impl From<[u8; 32]> for Sha256Hash {
24    fn from(hash: [u8; 32]) -> Self {
25        Sha256Hash(hash)
26    }
27}
28
29#[derive(Debug, Error)]
30pub enum WasmError {
31    #[error(transparent)]
32    Module(#[from] WasmModuleError),
33
34    #[error(transparent)]
35    Manager(#[from] WasmManagerError),
36
37    #[error(transparent)]
38    Runtime(#[from] WasmRuntimeError),
39
40    #[error(transparent)]
41    Io(#[from] std::io::Error),
42
43    #[error("{0}")]
44    Other(String),
45}
46
47#[derive(Debug, Error)]
48pub enum WasmModuleError {
49    #[error("invalid module descriptor: {0}")]
50    InvalidDescriptor(String),
51
52    #[error("module with same sha256 already exists: {0}")]
53    DuplicateSha256(Sha256Hash),
54
55    #[error("module not found: {0}")]
56    NotFound(uuid::Uuid),
57
58    #[error("failed to read module file: {0}")]
59    FileRead(String),
60
61    #[error("validation failed: {0}")]
62    ValidationFailed(String),
63
64    #[error("attach point missing: {0}")]
65    AttachPointMissing(String),
66
67    #[error("invalid function for attach point: {0}")]
68    AttachPointFunctionInvalid(String),
69}
70
71#[derive(Debug, Error)]
72pub enum WasmManagerError {
73    #[error("failed to acquire lock: {0}")]
74    LockFailed(String),
75
76    #[error("module add failed: {0}")]
77    ModuleAddFailed(String),
78
79    #[error("module remove failed: {0}")]
80    ModuleRemoveFailed(String),
81
82    #[error("runtime unavailable")]
83    RuntimeUnavailable,
84
85    #[error("execution failed: {0}")]
86    ExecutionFailed(String),
87
88    #[error("module {0} not found")]
89    ModuleNotFound(uuid::Uuid),
90}
91
92#[derive(Debug, Error)]
93pub enum WasmRuntimeError {
94    #[error("failed to create engine: {0}")]
95    EngineCreateFailed(String),
96
97    #[error("failed to compile module: {0}")]
98    CompileFailed(String),
99
100    #[error("failed to create instance: {0}")]
101    InstanceCreateFailed(String),
102
103    #[error("function not found: {0}")]
104    FunctionNotFound(String),
105
106    #[error("execution timeout after {0}ms")]
107    Timeout(u64),
108
109    #[error("execution failed: {0}")]
110    CallFailed(String),
111}
112
113impl From<wasmtime::Error> for WasmError {
114    fn from(value: wasmtime::Error) -> Self {
115        WasmError::Runtime(WasmRuntimeError::CallFailed(value.to_string()))
116    }
117}