plux_rs/utils/error.rs
1use semver::Version;
2use std::{
3 error::Error as StdError,
4 fmt::{Debug, Display},
5};
6use thiserror::Error;
7
8use crate::{Bundle, Depend};
9
10/// Errors that can occur when parsing a bundle from a filename.
11///
12/// This error type is returned by `Bundle::from_filename()` when the filename
13/// doesn't match the expected format `{id}-v{version}.{format}`.
14#[derive(Error, Debug)]
15pub enum BundleFromError {
16 /// Failed to convert OsStr to UTF-8 string
17 #[error("Error converting OsStr to str")]
18 OsStrToStrFailed,
19 /// Could not extract the plugin ID from the filename
20 #[error("Failed to get ID")]
21 IDFailed,
22 /// Could not extract the version from the filename
23 #[error("Failed to get version")]
24 VersionFailed,
25 /// Could not extract the format from the filename
26 #[error("Failed to get format")]
27 FormatFailed,
28 /// The version string is not a valid semantic version
29 #[error("Failed to parse version")]
30 ParseVersion(#[from] semver::Error),
31}
32
33/// Errors that can occur when creating a ZIP archive from a plugin bundle.
34///
35/// This error type is returned by the `zip` function when creating plugin archives.
36#[cfg(feature = "archive")]
37#[derive(Error, Debug)]
38pub enum BundleZipError {
39 /// The bundle has no name
40 #[error("Bundle has no name")]
41 NoNameFailed,
42 /// The bundle is missing
43 #[error("Missing bundle")]
44 MissingBundleFailed,
45 /// The directory contains a directory with the same name as the bundle
46 #[error("The directory contains a directory with the same name as the bundle")]
47 ContainSameDirFailed,
48 /// Failed to create the bundle archive
49 #[error("Failed to create bundle")]
50 CreateBundleFailed(std::io::Error),
51 /// Failed to open a file in the bundle
52 #[error("Failed to open file in bundle")]
53 OpenFileInBundleFailed(#[from] std::io::Error),
54 /// Failed to create the ZIP archive
55 #[error("Failed to zip")]
56 ZipFailed(#[from] zip::result::ZipError),
57}
58
59/// Errors that can occur when extracting a ZIP archive to a plugin bundle.
60///
61/// This error type is returned by the `unzip` function when extracting plugin archives.
62#[cfg(feature = "archive")]
63#[derive(Error, Debug)]
64pub enum BundleUnzipError {
65 /// The bundle has no name
66 #[error("Bundle has no name")]
67 NoNameFailed,
68 /// The bundle is missing
69 #[error("Missing bundle")]
70 MissingBundleFailed,
71 /// The directory contains a file with the same name as the bundle
72 #[error("The directory contains a file with the same name as the bundle")]
73 ContainSameFileFailed,
74 /// Failed to open the bundle archive
75 #[error("Failed to open bundle")]
76 OpenBundleFailed(#[from] std::io::Error),
77 /// Failed to extract the ZIP archive
78 #[error("Failed to unzip")]
79 UnzipFailed(#[from] zip::result::ZipError),
80 /// Error creating BundleInfo from the extracted files
81 #[error("Error creating BundleInfo")]
82 BundleFromFailed(#[from] BundleFromError),
83}
84
85/// Errors that can occur when stopping the plugin loader.
86///
87/// This error type is returned by `Loader::stop()` when cleanup operations fail.
88#[derive(Error, Debug)]
89pub enum StopLoaderError {
90 /// Failed to unregister one or more plugins
91 #[error("Failed to unregister plugins `{0:?}`")]
92 UnregisterPluginFailed(Vec<UnregisterPluginError>),
93 /// Failed to unregister one or more managers
94 #[error("Failed to unregister managers `{0:?}`")]
95 UnregisterManagerFailed(Vec<UnregisterManagerError>),
96}
97
98/// Errors that can occur when registering a plugin manager.
99///
100/// This error type is returned by manager registration operations.
101#[derive(Error, Debug)]
102pub enum RegisterManagerError {
103 /// A manager with the same format is already registered
104 #[error("Format `{0}` is already occupied")]
105 AlreadyOccupiedFormat(String),
106 /// The manager itself returned an error during registration
107 #[error("Manager registration error by the manager")]
108 RegisterManagerByManager(#[from] Box<dyn StdError + Send + Sync>),
109}
110
111/// Errors that can occur when unregistering a plugin manager.
112///
113/// This error type is returned by manager unregistration operations.
114#[derive(Error, Debug)]
115pub enum UnregisterManagerError {
116 /// The manager was not found
117 #[error("Not found manager")]
118 NotFound,
119 /// Failed to unregister a plugin during manager unregistration
120 #[error("Failed to unregister plugin")]
121 UnregisterPlugin(#[from] UnregisterPluginError),
122 /// The manager itself returned an error during unregistration
123 #[error("Manager unregistration error by the manager")]
124 UnregisterManagerByManager(#[from] Box<dyn StdError + Send + Sync>),
125}
126
127/// Errors that can occur when registering a plugin.
128///
129/// This error type is returned by plugin registration operations.
130#[derive(Error, Debug)]
131pub enum RegisterPluginError {
132 /// The plugin was not found
133 #[error("Not found plugin")]
134 NotFound,
135 /// Failed to parse bundle information from the filename
136 #[error("Failed to bundle from filename")]
137 BundleFromFailed(#[from] BundleFromError),
138 /// No manager exists for the plugin's format
139 #[error("Unknown plugin manager for the format '{0}'")]
140 UnknownManagerFormat(String),
141 /// The plugin manager returned an error during registration
142 #[error("Plugin registration error by the manager")]
143 RegisterPluginByManager(#[from] Box<dyn StdError + Send + Sync>),
144 /// A plugin with the same ID and version already exists
145 #[error("A plugin with ID `{0}` and version `{1}` already exists")]
146 AlreadyExistsIDAndVersion(String, Version),
147}
148
149/// Errors that can occur when unregistering a plugin.
150///
151/// This error type is returned by plugin unregistration operations.
152#[derive(Error, Debug)]
153pub enum UnregisterPluginError {
154 /// The plugin was not found
155 #[error("Not found plugin")]
156 NotFound,
157 /// Failed to unload the plugin during unregistration
158 #[error("Plugin unload error")]
159 UnloadError(#[from] UnloadPluginError),
160 /// The plugin's manager has been unregistered
161 #[error("The plugin has an unregistered manager")]
162 HasUnregisteredManager,
163 /// The plugin manager returned an error during unregistration
164 #[error("Plugin unregistration error by the manager")]
165 UnregisterPluginByManager(#[from] Box<dyn StdError + Send + Sync>),
166}
167
168/// Errors that can occur when loading a plugin.
169///
170/// This error type is returned by plugin loading operations.
171#[derive(Error, Debug)]
172pub enum LoadPluginError {
173 /// The plugin to load was not found
174 #[error("Not found plugin")]
175 NotFound,
176 /// One or more required dependencies could not be found
177 #[error("The following dependencies could not be found: {0:?}")]
178 NotFoundDependencies(Vec<Depend>),
179 /// A dependency failed to load
180 #[error("Dependency `{depend}` returned an error: {error:?}")]
181 LoadDependency {
182 /// The dependency that failed
183 depend: Depend,
184 /// The error that occurred while loading the dependency
185 error: Box<LoadPluginError>,
186 },
187 /// The plugin manager returned an error during loading
188 #[error("Plugin load error by the manager")]
189 LoadPluginByManager(#[from] Box<dyn StdError + Send + Sync>),
190 /// The plugin doesn't implement required function requests
191 #[error("Requests not found: {0:?}")]
192 RequestsNotFound(Vec<String>),
193}
194
195/// Errors that can occur when unloading a plugin.
196///
197/// This error type is returned by plugin unloading operations.
198#[derive(Error, Debug)]
199pub enum UnloadPluginError {
200 /// The plugin was not found
201 #[error("Not found plugin")]
202 NotFound,
203 /// The plugin is currently used as a dependency by another plugin
204 #[error("The plugin `{plugin}` currently uses the plugin `{depend}` as a dependency")]
205 CurrentlyUsesDepend {
206 /// The plugin that depends on the one being unloaded
207 plugin: Bundle,
208 /// The dependency that is being unloaded
209 depend: Bundle
210 },
211 /// The plugin manager returned an error during unloading
212 #[error("Plugin unload error by the manager")]
213 UnloadPluginByManager(#[from] Box<dyn StdError + Send + Sync>),
214}
215
216/// Error that can occur during plugin operations involving registration and loading.
217///
218/// This error type combines both registration and loading errors that can occur
219/// when performing operations like `load_plugin_now`.
220#[derive(Error, Debug)]
221pub enum PluginOperationError {
222 /// An error occurred during plugin registration
223 #[error("Plugin registration failed: {0}")]
224 Registration(#[from] RegisterPluginError),
225
226 /// An error occurred during plugin unregistration
227 #[error("Plugin unregistration failed: {0}")]
228 Unregistration(#[from] UnregisterPluginError),
229
230 /// An error occurred during plugin loading
231 #[error("Plugin loading failed: {0}")]
232 Loading(#[from] LoadPluginError),
233
234 /// An error occurred during plugin unloading
235 #[error("Plugin unloading failed: {0}")]
236 Unloading(#[from] UnloadPluginError),
237}
238
239/// Errors that can occur when registering a function request in a plugin.
240///
241/// This error type is returned when validating and registering function requests.
242#[derive(Error, Debug)]
243pub enum RegisterRequestError {
244 /// The requested function was not found
245 #[error("Function not found")]
246 NotFound,
247 /// The function arguments are incorrectly specified
248 #[error("The arguments are set incorrectly")]
249 ArgumentsIncorrectly,
250}
251
252/// Errors that can occur when calling a plugin request.
253///
254/// This error type is returned when attempting to call a function request on a plugin.
255#[derive(Error, Debug)]
256pub enum PluginCallRequestError {
257 /// The requested function was not found in the plugin
258 #[error("Request not found")]
259 NotFound,
260}
261
262/// Errors that can occur when registering a function in a plugin.
263///
264/// This error type is returned when attempting to register a function in a plugin's registry.
265#[derive(Error, Debug)]
266pub enum PluginRegisterFunctionError {
267 /// A function with the same name already exists in the registry
268 #[error("Function {0} already exists")]
269 AlreadyExists(String),
270}
271
272/// Errors that can occur when calling a function in a plugin.
273///
274/// This error type is returned when attempting to call a function in a plugin's registry.
275#[derive(Error, Debug)]
276pub enum PluginCallFunctionError {
277 /// The requested function was not found in the plugin's registry
278 #[error("Function not found")]
279 NotFound,
280}
281
282/// Errors that can occur when calling a function on a plugin dependency.
283///
284/// This error type is returned when attempting to call a function on a plugin's dependency.
285#[derive(Error, Debug)]
286pub enum CallFunctionDependError {
287 /// The required dependency was not found
288 #[error("Depend not found")]
289 DependNotFound,
290 /// Failed to call the function on the dependency
291 #[error("Failed to call function")]
292 FailedCallFunction(#[from] PluginCallFunctionError),
293}
294
295/// Result type for manager operations.
296///
297/// This type alias is used throughout the plugin system for operations that can fail.
298/// It provides a consistent error handling interface for manager implementations.
299pub type ManagerResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync>>;
300
301/// Error that occurs when parsing a Variable into a specific type.
302///
303/// This error is returned when attempting to convert a Variable to a specific
304/// Rust type, but the conversion fails due to a type mismatch.
305#[derive(Debug)]
306pub struct ParseVariableError {
307 ty: &'static str,
308}
309
310impl ParseVariableError {
311 /// Creates a new ParseVariableError with the specified type name.
312 ///
313 /// # Parameters
314 ///
315 /// * `ty` - The name of the type that the conversion failed for
316 ///
317 /// # Returns
318 ///
319 /// Returns a new ParseVariableError instance.
320 pub fn new(ty: &'static str) -> Self {
321 Self { ty }
322 }
323}
324
325impl Display for ParseVariableError {
326 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
327 write!(f, "data cannot be converted to this type `{}`", self.ty)
328 }
329}
330
331impl StdError for ParseVariableError {}