sos_core/
error.rs

1//! Errors generated by the core library.
2use thiserror::Error;
3
4use crate::VaultId;
5
6/// Error thrown by the core library.
7#[derive(Debug, Error)]
8pub enum Error {
9    /// Error generated when a commit tree is expected to have a root.
10    #[error("commit tree does not have a root")]
11    NoRootCommit,
12
13    /// Error generated when a commit tree is expected to have a last commit.
14    #[error("commit tree does not have a last commit")]
15    NoLastCommit,
16
17    /// Error generated when an external file could not be parsed.
18    #[error("external file reference '{0}' could not be parsed")]
19    InvalidExternalFile(String),
20
21    /// Error generated when the kind identifier of an event is unknown.
22    #[error("unknown event kind {0}")]
23    UnknownEventKind(u16),
24
25    /// Error generated when the kind identifier of an event is unknown.
26    #[error("unknown event type {0}")]
27    UnknownEventType(String),
28
29    /// Error generated when attempting to use an asymmetric
30    /// private key with a symmetric cipher.
31    #[error("symmetric private key required for symmetric cipher")]
32    NotSymmetric,
33
34    /// Error generated when attempting to use a symmetric
35    /// private key with an asymmetric cipher.
36    #[error("asymmetric private key required for asymmetric cipher")]
37    NotAsymmetric,
38
39    /// Error generated when a vault cipher string identifier is wrong.
40    #[error("invalid cipher {0}")]
41    InvalidCipher(String),
42
43    /// Error generated when an AeadPack contains a nonce that
44    /// is invalid for the decryption cipher.
45    #[error("invalid nonce")]
46    InvalidNonce,
47
48    /// Error generated when a vault key derivation function string
49    /// identifier is wrong.
50    #[error("invalid key derivation function {0}")]
51    InvalidKeyDerivation(String),
52
53    /// Error generated when an account identififer has
54    /// the wrong prefix.
55    #[error("account identifier must begin with 0x")]
56    BadAccountIdPrefix,
57
58    /// Error generated when a vault identity byte is wrong.
59    #[error("bad identity byte {0:#04x} at position {1} expecting {2}")]
60    BadIdentity(u8, usize, String),
61
62    /// Error generated when a buffer used to read identity bytes
63    /// is not long enough.
64    #[error("buffer passed for identity check is too short")]
65    IdentityLength,
66
67    /// Error generated when a a event log file does
68    /// not begin with a create vault event.
69    #[error("first record in an event log must be a create vault event")]
70    CreateEventMustBeFirst,
71
72    /// Error generated when a event log create vault event is not the first record.
73    #[error("event log create vault event must only be the first record")]
74    CreateEventOnlyFirst,
75
76    /// Generic boxed error.
77    #[error(transparent)]
78    Boxed(#[from] Box<dyn std::error::Error + Send + Sync>),
79
80    /// Error generated converting by the IO module.
81    #[error(transparent)]
82    Io(#[from] std::io::Error),
83
84    /// Authentication errors.
85    #[error(transparent)]
86    Authentication(#[from] AuthenticationError),
87
88    /// Error generated by the JSON library.
89    #[error(transparent)]
90    Json(#[from] serde_json::Error),
91
92    /// Error generated by the Base58 library.
93    #[error(transparent)]
94    Base58(#[from] bs58::encode::Error),
95
96    /// Error generated converting to fixed length slice.
97    #[error(transparent)]
98    TryFromSlice(#[from] std::array::TryFromSliceError),
99
100    /// Error generated converting from hexadecimal.
101    #[error(transparent)]
102    Hex(#[from] hex::FromHexError),
103
104    /// Error generated converting from UUID.
105    #[error(transparent)]
106    Uuid(#[from] uuid::Error),
107
108    /// Error generated converting time types.
109    #[error(transparent)]
110    Time(#[from] time::error::ComponentRange),
111
112    /// Error generated formatting time.
113    #[error(transparent)]
114    TimeFormat(#[from] time::error::Format),
115
116    /// Error generated parsing time.
117    #[error(transparent)]
118    TimeParse(#[from] time::error::Parse),
119
120    /// Error generated creating format descriptions for date formatting.
121    #[error(transparent)]
122    InvalidFormat(#[from] time::error::InvalidFormatDescription),
123
124    /// Error generated by the SHA2 library.
125    #[error(transparent)]
126    Sha2DigestLength(#[from] sha2::digest::InvalidLength),
127
128    /// Error generated parsing PEM files.
129    #[error(transparent)]
130    Pem(#[from] pem::PemError),
131
132    /// Error generated by the crypto library.
133    #[error(transparent)]
134    ChaCha(#[from] chacha20poly1305::Error),
135
136    /// Error generated by password hash.
137    #[error(transparent)]
138    PasswordHash(#[from] argon2::password_hash::Error),
139
140    /// Error generated by the AGE library when encrypting.
141    #[error(transparent)]
142    AgeEncrypt(#[from] age::EncryptError),
143
144    /// Error generated by the AGE library when decrypting.
145    #[error(transparent)]
146    AgeDecrypt(#[from] age::DecryptError),
147}
148
149/// Extension functions for error types.
150pub trait ErrorExt {
151    /// Whether this is a secret not found error.
152    fn is_secret_not_found(&self) -> bool;
153
154    /// Whether this is a permission denied error.
155    fn is_permission_denied(&self) -> bool;
156
157    /// Whether authentication is required.
158    fn is_forbidden(&self) -> bool;
159}
160
161/// Storage error shared between the client and server.
162#[derive(Debug, Error)]
163pub enum StorageError {
164    /// Error generated attempting to access a folder
165    /// that is not available in-memory.
166    #[error("folder not found '{0}'")]
167    FolderNotFound(VaultId),
168}
169
170/// Authentication errors.
171#[derive(Debug, Error)]
172pub enum AuthenticationError {
173    /// Error generated accessing an account that is not
174    /// authenticated when authentication is required.
175    #[error("account not authenticated, sign in required")]
176    NotAuthenticated,
177
178    /// Error generated when attempting to verify a password fails.
179    ///
180    /// This can happen when unlocking a vault or verifying a password.
181    #[error("password verification failed")]
182    PasswordVerification,
183}