mockforge_plugin_core/
error.rs

1//! Plugin error types and result handling
2
3/// Plugin system result type
4pub type Result<T> = std::result::Result<T, PluginError>;
5
6/// Comprehensive error types for the plugin system
7#[derive(Debug, thiserror::Error)]
8pub enum PluginError {
9    /// Plugin loading or validation failed
10    #[error("Plugin loading error: {message}")]
11    LoadError {
12        /// Error message describing what went wrong
13        message: String,
14    },
15
16    /// Plugin execution failed
17    #[error("Plugin execution error: {message}")]
18    ExecutionError {
19        /// Error message describing the execution failure
20        message: String,
21    },
22
23    /// Plugin violated security constraints
24    #[error("Security violation: {violation}")]
25    SecurityViolation {
26        /// Description of the security violation
27        violation: String,
28    },
29
30    /// Plugin exceeded resource limits
31    #[error("Resource limit exceeded: {resource} limit={limit}, used={used}")]
32    ResourceLimitExceeded {
33        /// The resource that exceeded its limit
34        resource: String,
35        /// The configured limit
36        limit: String,
37        /// The amount used
38        used: String,
39    },
40
41    /// Plugin configuration is invalid
42    #[error("Invalid plugin configuration: {field} - {message}")]
43    InvalidConfiguration {
44        /// The configuration field that is invalid
45        field: String,
46        /// Error message describing the configuration issue
47        message: String,
48    },
49
50    /// Plugin is incompatible with current system
51    #[error("Plugin compatibility error: {reason}")]
52    CompatibilityError {
53        /// Reason for the compatibility error
54        reason: String,
55    },
56
57    /// Plugin communication failed
58    #[error("Plugin communication error: {message}")]
59    CommunicationError {
60        /// Error message describing the communication failure
61        message: String,
62    },
63
64    /// Plugin timed out
65    #[error("Plugin execution timeout: {timeout_ms}ms exceeded")]
66    TimeoutError {
67        /// Timeout duration in milliseconds
68        timeout_ms: u64,
69    },
70
71    /// WebAssembly runtime error
72    #[error("WebAssembly runtime error: {message}")]
73    WasmError {
74        /// Error message from the WASM runtime
75        message: String,
76    },
77
78    /// Plugin manifest is invalid
79    #[error("Invalid plugin manifest: {message}")]
80    InvalidManifest {
81        /// Error message describing the manifest issue
82        message: String,
83    },
84
85    /// Plugin dependency not found or incompatible
86    #[error("Plugin dependency error: {dependency} - {message}")]
87    DependencyError {
88        /// The dependency that caused the error
89        dependency: String,
90        /// Error message describing the dependency issue
91        message: String,
92    },
93
94    /// Generic plugin system error
95    #[error("Plugin system error: {message}")]
96    SystemError {
97        /// Error message describing the system error
98        message: String,
99    },
100}
101
102impl PluginError {
103    /// Create a load error
104    pub fn load<S: Into<String>>(message: S) -> Self {
105        Self::LoadError {
106            message: message.into(),
107        }
108    }
109
110    /// Create an execution error
111    pub fn execution<S: Into<String>>(message: S) -> Self {
112        Self::ExecutionError {
113            message: message.into(),
114        }
115    }
116
117    /// Create a security violation error
118    pub fn security<S: Into<String>>(violation: S) -> Self {
119        Self::SecurityViolation {
120            violation: violation.into(),
121        }
122    }
123
124    /// Create a resource limit error
125    pub fn resource_limit<S: Into<String>>(resource: S, limit: S, used: S) -> Self {
126        Self::ResourceLimitExceeded {
127            resource: resource.into(),
128            limit: limit.into(),
129            used: used.into(),
130        }
131    }
132
133    /// Create a configuration error
134    pub fn config<S: Into<String>>(field: S, message: S) -> Self {
135        Self::InvalidConfiguration {
136            field: field.into(),
137            message: message.into(),
138        }
139    }
140
141    /// Create a compatibility error
142    pub fn compatibility<S: Into<String>>(reason: S) -> Self {
143        Self::CompatibilityError {
144            reason: reason.into(),
145        }
146    }
147
148    /// Create a communication error
149    pub fn communication<S: Into<String>>(message: S) -> Self {
150        Self::CommunicationError {
151            message: message.into(),
152        }
153    }
154
155    /// Create a timeout error
156    pub fn timeout(timeout_ms: u64) -> Self {
157        Self::TimeoutError { timeout_ms }
158    }
159
160    /// Create a WASM error
161    pub fn wasm<S: Into<String>>(message: S) -> Self {
162        Self::WasmError {
163            message: message.into(),
164        }
165    }
166
167    /// Create a manifest error
168    pub fn manifest<S: Into<String>>(message: S) -> Self {
169        Self::InvalidManifest {
170            message: message.into(),
171        }
172    }
173
174    /// Create a dependency error
175    pub fn dependency<S: Into<String>>(dependency: S, message: S) -> Self {
176        Self::DependencyError {
177            dependency: dependency.into(),
178            message: message.into(),
179        }
180    }
181
182    /// Create a system error
183    pub fn system<S: Into<String>>(message: S) -> Self {
184        Self::SystemError {
185            message: message.into(),
186        }
187    }
188
189    /// Check if this is a security-related error
190    pub fn is_security_error(&self) -> bool {
191        matches!(self, PluginError::SecurityViolation { .. })
192    }
193
194    /// Check if this is a resource-related error
195    pub fn is_resource_error(&self) -> bool {
196        matches!(self, PluginError::ResourceLimitExceeded { .. })
197    }
198
199    /// Check if this is a timeout error
200    pub fn is_timeout_error(&self) -> bool {
201        matches!(self, PluginError::TimeoutError { .. })
202    }
203}
204
205#[cfg(test)]
206mod tests {
207
208    #[test]
209    fn test_module_compiles() {
210        // Basic compilation test
211    }
212}