bitwarden_core/
error.rs

1//! Errors that can occur when using this SDK
2
3use std::fmt::Debug;
4
5use bitwarden_api_api::apis::Error as ApiApisError;
6use bitwarden_api_identity::apis::Error as IdentityError;
7#[cfg(feature = "internal")]
8use bitwarden_error::bitwarden_error;
9use reqwest::StatusCode;
10use thiserror::Error;
11
12macro_rules! impl_bitwarden_error {
13    ($name:ident, $error:ident) => {
14        impl<T> From<$name<T>> for $error {
15            fn from(e: $name<T>) -> Self {
16                match e {
17                    $name::Reqwest(e) => Self::Reqwest(e),
18                    $name::ResponseError(e) => Self::ResponseContent {
19                        status: e.status,
20                        message: e.content,
21                    },
22                    $name::Serde(e) => Self::Serde(e),
23                    $name::Io(e) => Self::Io(e),
24                }
25            }
26        }
27    };
28}
29
30/// Errors from performing network requests.
31#[allow(missing_docs)]
32#[derive(Debug, Error)]
33pub enum ApiError {
34    #[error(transparent)]
35    Reqwest(#[from] reqwest::Error),
36    #[error(transparent)]
37    Serde(#[from] serde_json::Error),
38    #[error(transparent)]
39    Io(#[from] std::io::Error),
40
41    #[error("Received error message from server: [{}] {}", .status, .message)]
42    ResponseContent { status: StatusCode, message: String },
43}
44
45impl_bitwarden_error!(ApiApisError, ApiError);
46impl_bitwarden_error!(IdentityError, ApiError);
47
48/// Client is not authenticated or the session has expired.
49#[derive(Debug, Error)]
50#[error("The client is not authenticated or the session has expired")]
51pub struct NotAuthenticatedError;
52
53/// Client's user ID is already set.
54#[derive(Debug, Error)]
55#[error("The client user ID is already set")]
56pub struct UserIdAlreadySetError;
57
58/// Missing required field.
59#[derive(Debug, Error)]
60#[error("The response received was missing a required field: {0}")]
61pub struct MissingFieldError(pub &'static str);
62
63/// Client vault is locked.
64#[derive(Debug, Error)]
65#[error("The client vault is locked and needs to be unlocked before use")]
66pub struct VaultLockedError;
67
68/// Wrong password.
69#[derive(Debug, thiserror::Error)]
70#[error("Wrong password")]
71pub struct WrongPasswordError;
72
73/// Missing private key.
74#[derive(Debug, thiserror::Error)]
75#[error("Missing private key")]
76pub struct MissingPrivateKeyError;
77
78/// Signifies that the state is invalid from a cryptographic perspective, such as a required
79/// security value missing, or being invalid
80#[cfg(feature = "internal")]
81#[bitwarden_error(flat)]
82#[derive(Debug, thiserror::Error)]
83pub enum StatefulCryptoError {
84    /// The security state is not present, but required for this user. V2 users must always
85    /// have a security state, V1 users cannot have a security state.
86    #[error("Security state is required, but missing")]
87    MissingSecurityState,
88    /// The function expected a user in a account cryptography version, but got a different one.
89    #[error("Expected user in account cryptography version {expected}, but got {got}")]
90    WrongAccountCryptoVersion {
91        /// The expected account cryptography version. This can include a range, such as `2+`.
92        expected: String,
93        /// The actual account cryptography version.
94        got: u32,
95    },
96    #[error("Crypto error, {0}")]
97    CryptoError(#[from] bitwarden_crypto::CryptoError),
98}
99
100/// This macro is used to require that a value is present or return an error otherwise.
101/// It is equivalent to using `val.ok_or(Error::MissingFields)?`, but easier to use and
102/// with a more descriptive error message.
103/// Note that this macro will return early from the function if the value is not present.
104#[macro_export]
105macro_rules! require {
106    ($val:expr) => {
107        match $val {
108            Some(val) => val,
109            None => return Err($crate::MissingFieldError(stringify!($val)).into()),
110        }
111    };
112}