Skip to main content

tauri_plugin_widgets/
error.rs

1//! Error types for the widget plugin.
2//!
3//! Provides a unified [`Error`] enum used across desktop and mobile implementations.
4//! All variants carry a human-readable message that is serialized to JSON when
5//! returned to the webview frontend.
6
7use serde::{Serialize, Serializer};
8
9/// Alias for `std::result::Result<T, Error>`.
10pub type Result<T> = std::result::Result<T, Error>;
11
12/// All possible errors produced by this plugin.
13#[derive(Debug, Clone)]
14pub enum Error {
15    /// An I/O error (file system, network, etc.).
16    Io(String),
17    /// A generic string error.
18    String(String),
19    /// An error originating from the native mobile plugin bridge.
20    #[cfg(mobile)]
21    PluginInvoke(String),
22    /// A JSON serialization / deserialization error.
23    SerdeJson(String),
24    /// The requested operation is not supported on the current platform.
25    Unsupported(String),
26}
27
28impl Error {
29    /// Create a generic [`Error::String`] from anything convertible to `String`.
30    pub fn new(msg: impl Into<String>) -> Self {
31        Error::String(msg.into())
32    }
33
34    /// Create an [`Error::Unsupported`] indicating that the operation is
35    /// unavailable on the current platform.
36    pub fn unsupported(operation: &str) -> Self {
37        Error::Unsupported(format!("`{}` is not supported on this platform", operation))
38    }
39}
40
41impl std::fmt::Display for Error {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            Error::Io(err) => write!(f, "IO error: {}", err),
45            Error::String(s) => write!(f, "{}", s),
46            #[cfg(mobile)]
47            Error::PluginInvoke(err) => write!(f, "Plugin invoke error: {}", err),
48            Error::SerdeJson(err) => write!(f, "Serde JSON error: {}", err),
49            Error::Unsupported(msg) => write!(f, "Unsupported: {}", msg),
50        }
51    }
52}
53
54impl std::error::Error for Error {}
55
56impl From<std::io::Error> for Error {
57    fn from(err: std::io::Error) -> Self {
58        Error::Io(err.to_string())
59    }
60}
61
62impl From<serde_json::Error> for Error {
63    fn from(err: serde_json::Error) -> Self {
64        Error::SerdeJson(err.to_string())
65    }
66}
67
68#[cfg(mobile)]
69impl From<tauri::plugin::mobile::PluginInvokeError> for Error {
70    fn from(err: tauri::plugin::mobile::PluginInvokeError) -> Self {
71        Error::PluginInvoke(err.to_string())
72    }
73}
74
75impl From<&str> for Error {
76    fn from(err: &str) -> Self {
77        Error::String(err.to_string())
78    }
79}
80
81impl From<String> for Error {
82    fn from(err: String) -> Self {
83        Error::String(err)
84    }
85}
86
87impl Serialize for Error {
88    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
89    where
90        S: Serializer,
91    {
92        serializer.serialize_str(&self.to_string())
93    }
94}
95
96unsafe impl Send for Error {}
97unsafe impl Sync for Error {}