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/// Errors that can occur when registering a function request in a plugin.
217///
218/// This error type is returned when validating and registering function requests.
219#[derive(Error, Debug)]
220pub enum RegisterRequestError {
221 /// The requested function was not found
222 #[error("Function not found")]
223 NotFound,
224 /// The function arguments are incorrectly specified
225 #[error("The arguments are set incorrectly")]
226 ArgumentsIncorrectly,
227}
228
229/// Errors that can occur when calling a plugin request.
230///
231/// This error type is returned when attempting to call a function request on a plugin.
232#[derive(Error, Debug)]
233pub enum PluginCallRequestError {
234 /// The requested function was not found in the plugin
235 #[error("Request not found")]
236 NotFound,
237}
238
239/// Errors that can occur when registering a function in a plugin.
240///
241/// This error type is returned when attempting to register a function in a plugin's registry.
242#[derive(Error, Debug)]
243pub enum PluginRegisterFunctionError {
244 /// A function with the same name already exists in the registry
245 #[error("Function {0} already exists")]
246 AlreadyExists(String),
247}
248
249/// Errors that can occur when calling a function in a plugin.
250///
251/// This error type is returned when attempting to call a function in a plugin's registry.
252#[derive(Error, Debug)]
253pub enum PluginCallFunctionError {
254 /// The requested function was not found in the plugin's registry
255 #[error("Function not found")]
256 NotFound,
257}
258
259/// Errors that can occur when calling a function on a plugin dependency.
260///
261/// This error type is returned when attempting to call a function on a plugin's dependency.
262#[derive(Error, Debug)]
263pub enum CallFunctionDependError {
264 /// The required dependency was not found
265 #[error("Depend not found")]
266 DependNotFound,
267 /// Failed to call the function on the dependency
268 #[error("Failed to call function")]
269 FailedCallFunction(#[from] PluginCallFunctionError),
270}
271
272/// Result type for manager operations.
273///
274/// This type alias is used throughout the plugin system for operations that can fail.
275/// It provides a consistent error handling interface for manager implementations.
276pub type ManagerResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync>>;
277
278/// Error that occurs when parsing a Variable into a specific type.
279///
280/// This error is returned when attempting to convert a Variable to a specific
281/// Rust type, but the conversion fails due to a type mismatch.
282#[derive(Debug)]
283pub struct ParseVariableError {
284 ty: &'static str,
285}
286
287impl ParseVariableError {
288 /// Creates a new ParseVariableError with the specified type name.
289 ///
290 /// # Parameters
291 ///
292 /// * `ty` - The name of the type that the conversion failed for
293 ///
294 /// # Returns
295 ///
296 /// Returns a new ParseVariableError instance.
297 pub fn new(ty: &'static str) -> Self {
298 Self { ty }
299 }
300}
301
302impl Display for ParseVariableError {
303 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
304 write!(f, "data cannot be converted to this type `{}`", self.ty)
305 }
306}
307
308impl StdError for ParseVariableError {}