Skip to main content

oo7/dbus/
error.rs

1/// DBus Secret Service specific errors.
2/// <https://specifications.freedesktop.org/secret-service-spec/latest/errors.html>
3#[derive(zbus::DBusError, Debug)]
4#[zbus(prefix = "org.freedesktop.Secret.Error")]
5pub enum ServiceError {
6    #[zbus(error)]
7    /// ZBus specific error.
8    ZBus(zbus::Error),
9    /// Collection/Item is locked.
10    IsLocked(String),
11    /// Session does not exist.
12    NoSession(String),
13    /// Collection/Item does not exist.
14    NoSuchObject(String),
15}
16
17/// DBus backend specific errors.
18#[derive(Debug)]
19pub enum Error {
20    /// Something went wrong on the wire.
21    ZBus(zbus::Error),
22    /// A service error.
23    Service(ServiceError),
24    /// The item/collection was removed.
25    Deleted,
26    /// The prompt request was dismissed.
27    Dismissed,
28    /// The collection doesn't exists
29    NotFound(String),
30    /// Input/Output.
31    IO(std::io::Error),
32    /// Crypto related error.
33    Crypto(crate::crypto::Error),
34    /// Schema error.
35    #[cfg(feature = "schema")]
36    Schema(crate::SchemaError),
37}
38
39impl From<zbus::Error> for Error {
40    fn from(e: zbus::Error) -> Self {
41        Self::ZBus(e)
42    }
43}
44
45impl From<zbus::fdo::Error> for Error {
46    fn from(e: zbus::fdo::Error) -> Self {
47        Self::ZBus(zbus::Error::FDO(Box::new(e)))
48    }
49}
50
51impl From<zbus::zvariant::Error> for Error {
52    fn from(e: zbus::zvariant::Error) -> Self {
53        Self::ZBus(zbus::Error::Variant(e))
54    }
55}
56
57impl From<ServiceError> for Error {
58    fn from(e: ServiceError) -> Self {
59        Self::Service(e)
60    }
61}
62
63impl From<std::io::Error> for Error {
64    fn from(e: std::io::Error) -> Self {
65        Self::IO(e)
66    }
67}
68
69impl From<crate::crypto::Error> for Error {
70    fn from(value: crate::crypto::Error) -> Self {
71        Self::Crypto(value)
72    }
73}
74
75#[cfg(feature = "schema")]
76impl From<crate::SchemaError> for Error {
77    fn from(value: crate::SchemaError) -> Self {
78        Self::Schema(value)
79    }
80}
81
82impl std::error::Error for Error {}
83
84impl std::fmt::Display for Error {
85    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        match self {
87            Self::ZBus(err) => write!(f, "zbus error {err}"),
88            Self::Service(err) => write!(f, "service error {err}"),
89            Self::IO(err) => write!(f, "IO error {err}"),
90            Self::Deleted => write!(f, "Item/Collection was deleted, can no longer be used"),
91            Self::NotFound(name) => write!(f, "The collection '{name}' doesn't exists"),
92            Self::Dismissed => write!(f, "Prompt was dismissed"),
93            Self::Crypto(e) => write!(f, "Failed to do a cryptography operation, {e}"),
94            #[cfg(feature = "schema")]
95            Self::Schema(e) => write!(f, "Schema error: {e}"),
96        }
97    }
98}