sos_account/
error.rs

1//! Errors generated by the account library.
2use sos_core::{AuthenticationError, ErrorExt, VaultId};
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Error generated by the account library.
7#[derive(Debug, Error)]
8pub enum Error {
9    /// Error generated when a folder password could not be located.
10    #[error("could not find folder password for '{0}'")]
11    NoFolderPassword(VaultId),
12
13    /// Error generated when a database is required.
14    #[error("database client is required")]
15    NoDatabase,
16
17    /// Error generated when a path is not a file.
18    #[error("path {0} is not a file")]
19    NotFile(PathBuf),
20
21    /// Error generated when no default folder is available.
22    #[error("no default folder")]
23    NoDefaultFolder,
24
25    /// Error generated when an open folder is expected.
26    #[error("no open folder")]
27    NoOpenFolder,
28
29    /// Error generated when a PEM-encoded certificate is invalid.
30    #[error("invalid PEM encoding")]
31    PemEncoding,
32
33    /// Error generated when an archive folder is not available.
34    #[error("archive folder does not exist")]
35    NoArchive,
36
37    /// Error generated when attempting to unarchive a secret that
38    /// is not archived.
39    #[error("cannot unarchive, not archived")]
40    NotArchived,
41
42    /// Error generated when attempting to archive a secret that
43    /// is already archived.
44    #[error("cannot move to archive, already archived")]
45    AlreadyArchived,
46
47    /// Error generated when a contacts folder is not available.
48    #[cfg(feature = "contacts")]
49    #[error("no contacts folder")]
50    NoContactsFolder,
51
52    /// Error generated when a secret is not a contact secret.
53    #[cfg(feature = "contacts")]
54    #[error("not a contact")]
55    NotContact,
56
57    /// Error generated by the JSON path library when no nodes matched.
58    #[cfg(feature = "clipboard")]
59    #[error("paths '{0:?}' did not match any nodes")]
60    JsonPathQueryEmpty(Vec<String>),
61
62    #[cfg(feature = "clipboard")]
63    /// Error when no clipboard is configured.
64    #[error("clipboard is not configured")]
65    NoClipboard,
66
67    /// Error generated converting to fixed length slice.
68    #[error(transparent)]
69    TryFromSlice(#[from] std::array::TryFromSliceError),
70
71    /// Error generated by the core library.
72    #[error(transparent)]
73    Core(#[from] sos_core::Error),
74
75    /// Authentication errors.
76    #[error(transparent)]
77    Authentication(#[from] sos_core::AuthenticationError),
78
79    #[cfg(feature = "search")]
80    /// Error generated by the search library.
81    #[error(transparent)]
82    Search(#[from] sos_search::Error),
83
84    /// Error generated by the vault library.
85    #[error(transparent)]
86    Vault(#[from] sos_vault::Error),
87
88    /// Error generated by the login library.
89    #[error(transparent)]
90    Login(#[from] sos_login::Error),
91
92    /// Error generated by the signer library.
93    #[error(transparent)]
94    Signer(#[from] sos_signer::Error),
95
96    /// Error generated by the sync library.
97    #[error(transparent)]
98    Sync(#[from] sos_sync::Error),
99
100    /// Error generated by the password library.
101    #[error(transparent)]
102    Password(#[from] sos_password::Error),
103
104    /// Error generated by the backend storage .
105    #[error(transparent)]
106    BackendStorage(#[from] sos_backend::StorageError),
107
108    /// Error generated by the storage library.
109    #[error(transparent)]
110    Storage(#[from] sos_client_storage::Error),
111
112    /// Error generated by the backend library.
113    #[error(transparent)]
114    Backend(#[from] sos_backend::Error),
115
116    /// Error generated by the database library.
117    #[error(transparent)]
118    Database(#[from] sos_database::Error),
119
120    /// Error generated by the database archive library.
121    #[cfg(feature = "archive")]
122    #[error(transparent)]
123    Archive(#[from] sos_database::archive::Error),
124
125    /// Error generated by the IO module.
126    #[error(transparent)]
127    Io(#[from] std::io::Error),
128
129    /// Error generated by the JSON library.
130    #[error(transparent)]
131    Json(#[from] serde_json::Error),
132
133    /// Error generated by the UUID library.
134    #[error(transparent)]
135    Uuid(#[from] uuid::Error),
136
137    /// Error generated by the hexadecimal library.
138    #[error(transparent)]
139    Hex(#[from] hex::FromHexError),
140
141    /// Error generated by the VCard library.
142    #[cfg(feature = "contacts")]
143    #[error(transparent)]
144    Vcard(#[from] vcard4::Error),
145
146    /// Error generated by the clipboard library.
147    #[cfg(feature = "clipboard")]
148    #[error(transparent)]
149    Clipboard(#[from] xclipboard::Error),
150
151    /// Error generated attempting to detect the system time zone.
152    #[cfg(feature = "clipboard")]
153    #[error(transparent)]
154    TimeZone(#[from] time_tz::system::Error),
155
156    /// Error generated when stripping a prefix from a path.
157    #[cfg(feature = "files")]
158    #[error(transparent)]
159    StripPrefix(#[from] std::path::StripPrefixError),
160
161    /// Error generated by the migrate library.
162    #[cfg(feature = "migrate")]
163    #[error(transparent)]
164    Migrate(#[from] sos_migrate::Error),
165
166    /// Error generated by the backup archive library.
167    #[cfg(feature = "archive")]
168    #[error(transparent)]
169    BackupArchive(#[from] sos_filesystem::archive::Error),
170}
171
172impl ErrorExt for Error {
173    fn is_secret_not_found(&self) -> bool {
174        matches!(
175            self,
176            Error::Storage(sos_client_storage::Error::SecretNotFound(_))
177        )
178    }
179
180    fn is_forbidden(&self) -> bool {
181        matches!(
182            self,
183            Error::Authentication(AuthenticationError::NotAuthenticated)
184                | Error::Storage(sos_client_storage::Error::Authentication(
185                    AuthenticationError::NotAuthenticated
186                ))
187        )
188    }
189
190    fn is_permission_denied(&self) -> bool {
191        matches!(
192            self,
193            Error::Vault(sos_vault::Error::Authentication(
194                AuthenticationError::PasswordVerification
195            ))
196        )
197    }
198}