tauri_plugin_sharekit/
error.rs

1use serde::{ser::Serializer, Serialize};
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5/// Replica of the tauri::plugin::mobile::ErrorResponse for desktop platforms.
6#[cfg(desktop)]
7#[derive(Debug, thiserror::Error, Clone, serde::Deserialize)]
8pub struct ErrorResponse<T = ()> {
9    /// Error code.
10    pub code: Option<String>,
11    /// Error message.
12    pub message: Option<String>,
13    /// Optional error data.
14    #[serde(flatten)]
15    pub data: T,
16}
17
18#[cfg(desktop)]
19impl<T> std::fmt::Display for ErrorResponse<T> {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        if let Some(code) = &self.code {
22            write!(f, "[{code}]")?;
23            if self.message.is_some() {
24                write!(f, " - ")?;
25            }
26        }
27        if let Some(message) = &self.message {
28            write!(f, "{message}")?;
29        }
30        Ok(())
31    }
32}
33
34/// Replica of the tauri::plugin::mobile::PluginInvokeError for desktop platforms.
35#[cfg(desktop)]
36#[derive(Debug, thiserror::Error)]
37pub enum PluginInvokeError {
38    /// Error returned from direct desktop plugin.
39    #[error(transparent)]
40    InvokeRejected(#[from] ErrorResponse),
41    /// Failed to deserialize response.
42    #[error("failed to deserialize response: {0}")]
43    CannotDeserializeResponse(serde_json::Error),
44    /// Failed to serialize request payload.
45    #[error("failed to serialize payload: {0}")]
46    CannotSerializePayload(serde_json::Error),
47}
48
49#[derive(Debug, thiserror::Error)]
50pub enum Error {
51    #[error(transparent)]
52    Io(#[from] std::io::Error),
53    #[cfg(mobile)]
54    #[error(transparent)]
55    PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
56    #[cfg(desktop)]
57    #[error(transparent)]
58    PluginInvoke(#[from] crate::error::PluginInvokeError),
59    #[error("This feature is not supported on this platform")]
60    UnsupportedPlatform,
61    #[error("Window not found")]
62    WindowNotFound,
63    #[cfg(target_os = "windows")]
64    #[error("Windows API error: {0}")]
65    WindowsApi(String),
66}
67
68impl Serialize for Error {
69    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
70    where
71        S: Serializer,
72    {
73        serializer.serialize_str(self.to_string().as_ref())
74    }
75}