tauri_plugin_app_control/
error.rs

1use serde::{ser::Serializer, Serialize};
2
3#[cfg(mobile)] // Conditionally compile this import
4use tauri::plugin::mobile::PluginInvokeError;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10    #[error(transparent)]
11    Io(#[from] std::io::Error),
12    
13    #[error(transparent)]
14    PluginApi(#[from] tauri::Error),
15
16    #[cfg(mobile)] // Conditionally compile this variant
17    #[error(transparent)]
18    PluginInvoke(#[from] PluginInvokeError),
19    
20    #[error("Failed to minimize app")]
21    MinimizeFailed,
22    
23    #[error("Failed to close app")]
24    CloseFailed,
25    
26    #[error("Failed to exit app")]
27    ExitFailed,
28    
29    #[error("Invalid context")]
30    InvalidContext,
31
32    #[error("Unsupported platform: {0}")]
33    UnsupportedPlatform(String),
34    
35    #[error("Unknown error: {0}")]
36    Unknown(String),
37}
38
39impl Serialize for Error {
40    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
41    where
42        S: Serializer,
43    {
44        serializer.serialize_str(self.to_string().as_ref())
45    }
46}
47
48impl Error {
49    pub fn error_code(&self) -> &'static str {
50        match self {
51            Error::MinimizeFailed => "minimizeFailed",
52            Error::CloseFailed => "closeFailed",
53            Error::ExitFailed => "exitFailed",
54            Error::InvalidContext => "invalidContext",
55            Error::Io(_) => "ioError",
56            Error::PluginApi(_) => "pluginError",
57            #[cfg(mobile)] // Conditionally compile this match arm
58            Error::PluginInvoke(_) => "pluginInvokeError",
59            Error::UnsupportedPlatform(_) => "unsupportedPlatform",
60            Error::Unknown(_) => "unknownError",
61        }
62    }
63}