use std::{
error::Error,
fmt::{self, Debug, Display, Formatter},
};
#[cfg(all(target_os = "linux", feature = "linux-secret-service"))]
use secret_service::Error as LinuxOsError;
#[cfg(all(target_os = "macos", feature = "macos-keychain"))]
use security_framework::base::Error as MacOsError;
#[derive(Clone, Eq, PartialEq)]
pub enum KeyRingError {
ItemNotFound,
AccessDenied {
msg: String,
},
GeneralError {
msg: String,
},
}
impl KeyRingError {
pub fn as_str(&self) -> String {
match *self {
Self::ItemNotFound => {
"The specified item could not be found in the keychain".to_string()
}
Self::AccessDenied { ref msg } => format!("Unable to access the keychain: {:?}", msg),
Self::GeneralError { ref msg } => msg.to_string(),
}
}
}
impl Display for KeyRingError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl Debug for KeyRingError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", stringify!(self), self.as_str())
}
}
impl From<&str> for KeyRingError {
fn from(s: &str) -> Self {
KeyRingError::GeneralError { msg: s.to_string() }
}
}
impl Error for KeyRingError {}
impl From<std::io::Error> for KeyRingError {
fn from(value: std::io::Error) -> Self {
KeyRingError::GeneralError {
msg: value.to_string(),
}
}
}
#[cfg(all(target_os = "macos", feature = "macos-keychain"))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "macos", feature = "macos-keychain"))))]
impl From<MacOsError> for KeyRingError {
fn from(e: MacOsError) -> Self {
match e.code() {
-128 => KeyRingError::AccessDenied {
msg: format!("{:?}", e.to_string()),
},
-25300 => KeyRingError::ItemNotFound,
_ => KeyRingError::GeneralError {
msg: "Unknown error".to_string(),
},
}
}
}
#[cfg(all(target_os = "linux", feature = "linux-secret-service"))]
#[cfg_attr(
docsrs,
doc(cfg(all(target_os = "linux", feature = "linux-secret-service")))
)]
impl From<LinuxOsError> for KeyRingError {
fn from(e: LinuxOsError) -> Self {
match e {
LinuxOsError::Crypto(_)
| LinuxOsError::Zbus(_)
| LinuxOsError::ZbusFdo(_)
| LinuxOsError::Prompt
| LinuxOsError::PromptDisconnected
| LinuxOsError::Unavailable => KeyRingError::GeneralError {
msg: format!("{:?}", e.to_string()),
},
LinuxOsError::Locked => KeyRingError::AccessDenied {
msg: format!("{:?}", e.to_string()),
},
LinuxOsError::NoResult => KeyRingError::ItemNotFound,
_ => KeyRingError::GeneralError {
msg: "Unknown error".to_string(),
},
}
}
}
#[cfg(any(feature = "file", feature = "encrypted-vfs"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "file", feature = "encrypted-vfs"))))]
impl From<rusqlite::Error> for KeyRingError {
fn from(value: rusqlite::Error) -> Self {
KeyRingError::GeneralError {
msg: value.to_string(),
}
}
}
#[cfg(any(feature = "indexeddb", feature = "turso-opfs", feature = "web-storage"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "indexeddb", feature = "turso-opfs", feature = "web-storage")))
)]
impl From<wasm_bindgen::JsValue> for KeyRingError {
fn from(value: wasm_bindgen::JsValue) -> Self {
let detail = value
.as_string()
.or_else(|| {
js_sys::Reflect::get(&value, &wasm_bindgen::JsValue::from_str("message"))
.ok()
.and_then(|message| message.as_string())
})
.unwrap_or_else(|| format!("{value:?}"));
KeyRingError::GeneralError { msg: detail }
}
}
#[cfg(any(feature = "turso", feature = "turso-encrypted-io"))]
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "turso", feature = "turso-encrypted-io")))
)]
impl From<::turso::Error> for KeyRingError {
fn from(value: ::turso::Error) -> Self {
KeyRingError::GeneralError {
msg: value.to_string(),
}
}
}