Skip to main content

tauri_plugin_configurate/
error.rs

1use serde::{ser::Serializer, Serialize};
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug, thiserror::Error)]
6pub enum Error {
7    #[error(transparent)]
8    Io(#[from] std::io::Error),
9
10    /// Errors originating from storage serialization/deserialization.
11    #[error("storage error: {0}")]
12    Storage(String),
13
14    /// Errors originating from the OS keyring.
15    #[error("keyring error: {0}")]
16    Keyring(String),
17
18    /// Errors when resolving a dotpath inside a JSON value.
19    #[error("dotpath error: {0}")]
20    Dotpath(String),
21
22    /// Invalid payload sent from the frontend (wrong field combination, bad value, etc.).
23    #[error("invalid payload: {0}")]
24    InvalidPayload(String),
25
26    /// Errors from serde_json.
27    #[error(transparent)]
28    Json(#[from] serde_json::Error),
29
30    #[cfg(mobile)]
31    #[error(transparent)]
32    PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
33}
34
35impl From<keyring::Error> for Error {
36    fn from(e: keyring::Error) -> Self {
37        Error::Keyring(e.to_string())
38    }
39}
40
41impl Serialize for Error {
42    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
43    where
44        S: Serializer,
45    {
46        use serde::ser::SerializeMap;
47        let mut map = serializer.serialize_map(Some(2))?;
48        let kind = match self {
49            Error::Io(_) => "io",
50            Error::Storage(_) => "storage",
51            Error::Keyring(_) => "keyring",
52            Error::Dotpath(_) => "dotpath",
53            Error::InvalidPayload(_) => "invalid_payload",
54            Error::Json(_) => "json",
55            #[cfg(mobile)]
56            Error::PluginInvoke(_) => "plugin_invoke",
57        };
58        map.serialize_entry("kind", kind)?;
59        map.serialize_entry("message", &self.to_string())?;
60        map.end()
61    }
62}