Skip to main content

bitwarden_core/
error.rs

1//! Errors that can occur when using this SDK
2
3pub use bitwarden_api_base::Error as ApiError;
4#[cfg(feature = "internal")]
5use bitwarden_error::bitwarden_error;
6use thiserror::Error;
7
8/// Client is not authenticated or the session has expired.
9#[derive(Debug, Error)]
10#[error("The client is not authenticated or the session has expired")]
11pub struct NotAuthenticatedError;
12
13/// Client's user ID is already set.
14#[derive(Debug, Error, serde::Serialize, serde::Deserialize, Clone)]
15#[error("The client user ID is already set")]
16pub struct UserIdAlreadySetError;
17
18/// Missing required field.
19#[derive(Debug, Error)]
20#[error("The response received was missing a required field: {0}")]
21pub struct MissingFieldError(pub &'static str);
22
23/// Wrong password.
24#[derive(Debug, thiserror::Error)]
25#[error("Wrong password")]
26pub struct WrongPasswordError;
27
28/// Missing private key.
29#[derive(Debug, thiserror::Error)]
30#[error("Missing private key")]
31pub struct MissingPrivateKeyError;
32
33/// Signifies that the state is invalid from a cryptographic perspective, such as a required
34/// security value missing, or being invalid
35#[cfg(feature = "internal")]
36#[bitwarden_error(flat)]
37#[derive(Debug, thiserror::Error)]
38pub enum StatefulCryptoError {
39    /// The security state is not present, but required for this user. V2 users must always
40    /// have a security state, V1 users cannot have a security state.
41    #[error("Security state is required, but missing")]
42    MissingSecurityState,
43    /// The function expected a user in a account cryptography version, but got a different one.
44    #[error("Expected user in account cryptography version {expected}, but got {got}")]
45    WrongAccountCryptoVersion {
46        /// The expected account cryptography version. This can include a range, such as `2+`.
47        expected: String,
48        /// The actual account cryptography version.
49        got: u32,
50    },
51    #[error("Crypto error, {0}")]
52    Crypto(#[from] bitwarden_crypto::CryptoError),
53}
54
55/// This macro is used to require that a value is present or return an error otherwise.
56/// It is equivalent to using `val.ok_or(Error::MissingFields)?`, but easier to use and
57/// with a more descriptive error message.
58/// Note that this macro will return early from the function if the value is not present.
59#[macro_export]
60macro_rules! require {
61    ($val:expr) => {
62        match $val {
63            Some(val) => val,
64            None => return Err($crate::MissingFieldError(stringify!($val)).into()),
65        }
66    };
67}