sos_client_storage/
error.rs

1use sos_core::{AuthenticationError, ErrorExt, SecretId, VaultId};
2use std::path::PathBuf;
3use thiserror::Error;
4
5/// Errors generated by the client storage library.
6#[derive(Debug, Error)]
7pub enum Error {
8    /// Error generated attempting to make changes to the current
9    /// vault but no vault is open.
10    #[error("no vault is available, vault must be open")]
11    NoOpenVault,
12
13    /// Error generated when a directory is expected.
14    #[error("path {0} is not a directory")]
15    NotDirectory(PathBuf),
16
17    /// Error generated when a file secret is expected.
18    #[error("not a file secret")]
19    NotFileContent,
20
21    /// Error generated if we could not determine a cache directory.
22    #[error("could not determine cache directory")]
23    NoCache,
24
25    /// Error generated when a folder password in the identity
26    /// vault could not be located.
27    #[error("could not find folder password for '{0}'")]
28    NoFolderPassword(VaultId),
29
30    /// Error generated when a file encryption password is required.
31    #[error("no file password")]
32    NoFilePassword,
33
34    /// Error generated when a secret could not be found.
35    #[error(r#"secret "{0}" not found"#)]
36    SecretNotFound(SecretId),
37
38    /// Error generated if we could not find a create vault event
39    /// in a collection of event records or as the first event in
40    /// a folder event log.
41    #[error("could not find create vault event")]
42    NoVaultEvent,
43
44    /// Error generated converting to fixed length slice.
45    #[error(transparent)]
46    TryFromSlice(#[from] std::array::TryFromSliceError),
47
48    /// Error generated by the filesystem library.
49    #[error(transparent)]
50    FileSystem(#[from] sos_filesystem::Error),
51
52    /// Error generated by the vault library.
53    #[error(transparent)]
54    Vault(#[from] sos_vault::Error),
55
56    /// Error generated by the login library.
57    #[error(transparent)]
58    Login(#[from] sos_login::Error),
59
60    /// Errors generated by the core library.
61    #[error(transparent)]
62    Core(#[from] sos_core::Error),
63
64    /// Authentication errors.
65    #[error(transparent)]
66    Authentication(#[from] sos_core::AuthenticationError),
67
68    #[cfg(feature = "search")]
69    /// Errors generated by the search library.
70    #[error(transparent)]
71    Search(#[from] sos_search::Error),
72
73    /// Errors generated by the password library.
74    #[error(transparent)]
75    Password(#[from] sos_password::Error),
76
77    /// Errors generated by the sync library.
78    #[error(transparent)]
79    Sync(#[from] sos_sync::Error),
80
81    /// Errors generated by the backend storage.
82    #[error(transparent)]
83    BackendStorage(#[from] sos_backend::StorageError),
84
85    /// Errors generated by the backend library.
86    #[error(transparent)]
87    Backend(#[from] sos_backend::Error),
88
89    /// Errors generated by the database library.
90    #[error(transparent)]
91    Database(#[from] sos_database::Error),
92
93    /// Errors generated by the IO module.
94    #[error(transparent)]
95    Io(#[from] std::io::Error),
96
97    /// Error generated by the AGE library when encrypting.
98    #[cfg(feature = "files")]
99    #[error(transparent)]
100    AgeEncrypt(#[from] age::EncryptError),
101
102    /// Error generated by the AGE library when decrypting.
103    #[cfg(feature = "files")]
104    #[error(transparent)]
105    AgeDecrypt(#[from] age::DecryptError),
106}
107
108impl ErrorExt for Error {
109    fn is_secret_not_found(&self) -> bool {
110        matches!(self, Error::SecretNotFound(_))
111    }
112
113    fn is_forbidden(&self) -> bool {
114        matches!(
115            self,
116            Error::Authentication(AuthenticationError::NotAuthenticated)
117        )
118    }
119
120    fn is_permission_denied(&self) -> bool {
121        matches!(
122            self,
123            Error::Vault(sos_vault::Error::Authentication(
124                AuthenticationError::PasswordVerification
125            ))
126        )
127    }
128}