Skip to main content

barbacane_wasm/
error.rs

1//! Error types for the WASM runtime.
2
3use thiserror::Error;
4
5/// Errors that can occur in the WASM runtime.
6#[derive(Debug, Error)]
7pub enum WasmError {
8    /// Failed to create the WASM engine.
9    #[error("failed to create WASM engine: {0}")]
10    EngineCreation(String),
11
12    /// Failed to compile a WASM module.
13    #[error("failed to compile WASM module: {0}")]
14    Compilation(String),
15
16    /// Failed to instantiate a WASM module.
17    #[error("failed to instantiate WASM module: {0}")]
18    Instantiation(String),
19
20    /// WASM execution trapped.
21    #[error("WASM execution trapped: {0}")]
22    Trap(String),
23
24    /// WASM execution timed out.
25    #[error("WASM execution timed out after {0}ms")]
26    Timeout(u64),
27
28    /// WASM memory limit exceeded.
29    #[error("WASM memory limit exceeded: requested {requested} bytes, limit {limit} bytes")]
30    MemoryLimitExceeded { requested: usize, limit: usize },
31
32    /// Missing required export.
33    #[error("missing required WASM export: {0}")]
34    MissingExport(String),
35
36    /// Invalid export signature.
37    #[error("invalid export signature for '{name}': expected {expected}, got {actual}")]
38    InvalidExportSignature {
39        name: String,
40        expected: String,
41        actual: String,
42    },
43
44    /// Undeclared host function import.
45    #[error("plugin imports undeclared host function: {0}")]
46    UndeclaredImport(String),
47
48    /// Unknown capability.
49    #[error("unknown capability: {0}")]
50    UnknownCapability(String),
51
52    /// Plugin manifest parsing failed.
53    #[error("failed to parse plugin manifest: {0}")]
54    ManifestParse(String),
55
56    /// Plugin manifest validation failed.
57    #[error("invalid plugin manifest: {0}")]
58    ManifestValidation(String),
59
60    /// Config schema parsing failed.
61    #[error("failed to parse config schema: {0}")]
62    SchemaParse(String),
63
64    /// Config validation failed.
65    #[error("config validation failed: {0}")]
66    ConfigValidation(String),
67
68    /// Plugin initialization failed.
69    #[error("plugin initialization failed: {0}")]
70    InitFailed(String),
71
72    /// Invalid return code from plugin.
73    #[error("invalid return code from plugin: {0}")]
74    InvalidReturnCode(i32),
75
76    /// Failed to serialize/deserialize data.
77    #[error("serialization error: {0}")]
78    Serialization(String),
79
80    /// I/O error.
81    #[error("I/O error: {0}")]
82    Io(#[from] std::io::Error),
83}
84
85impl From<wasmtime::Error> for WasmError {
86    fn from(err: wasmtime::Error) -> Self {
87        WasmError::Compilation(err.to_string())
88    }
89}
90
91impl From<serde_json::Error> for WasmError {
92    fn from(err: serde_json::Error) -> Self {
93        WasmError::Serialization(err.to_string())
94    }
95}
96
97impl From<toml::de::Error> for WasmError {
98    fn from(err: toml::de::Error) -> Self {
99        WasmError::ManifestParse(err.to_string())
100    }
101}