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;
4use thiserror::Error;
5
6/// Client is not authenticated or the session has expired.
7#[derive(Debug, Error)]
8#[error("The client is not authenticated or the session has expired")]
9pub struct NotAuthenticatedError;
10
11/// Client's user ID is already set.
12#[derive(Debug, Error, serde::Serialize, serde::Deserialize, Clone)]
13#[error("The client user ID is already set")]
14pub struct UserIdAlreadySetError;
15
16/// Missing required field.
17#[derive(Debug, Error)]
18#[error("The response received was missing a required field: {0}")]
19pub struct MissingFieldError(pub &'static str);
20
21/// Wrong password.
22#[derive(Debug, thiserror::Error)]
23#[error("Wrong password")]
24pub struct WrongPasswordError;
25
26/// Missing private key.
27#[derive(Debug, thiserror::Error)]
28#[error("Missing private key")]
29pub struct MissingPrivateKeyError;
30
31/// This macro is used to require that a value is present or return an error otherwise.
32/// It is equivalent to using `val.ok_or(Error::MissingFields)?`, but easier to use and
33/// with a more descriptive error message.
34/// Note that this macro will return early from the function if the value is not present.
35#[macro_export]
36macro_rules! require {
37    ($val:expr) => {
38        match $val {
39            Some(val) => val,
40            None => return Err($crate::MissingFieldError(stringify!($val)).into()),
41        }
42    };
43}