1use 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#[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#[derive(Debug, Error)]
50#[error("The client is not authenticated or the session has expired")]
51pub struct NotAuthenticatedError;
52
53#[derive(Debug, Error)]
55#[error("The client user ID is already set")]
56pub struct UserIdAlreadySetError;
57
58#[derive(Debug, Error)]
60#[error("The response received was missing a required field: {0}")]
61pub struct MissingFieldError(pub &'static str);
62
63#[derive(Debug, Error)]
65#[error("The client vault is locked and needs to be unlocked before use")]
66pub struct VaultLockedError;
67
68#[derive(Debug, thiserror::Error)]
70#[error("Wrong password")]
71pub struct WrongPasswordError;
72
73#[derive(Debug, thiserror::Error)]
75#[error("Missing private key")]
76pub struct MissingPrivateKeyError;
77
78#[cfg(feature = "internal")]
81#[bitwarden_error(flat)]
82#[derive(Debug, thiserror::Error)]
83pub enum StatefulCryptoError {
84 #[error("Security state is required, but missing")]
87 MissingSecurityState,
88 #[error("Expected user in account cryptography version {expected}, but got {got}")]
90 WrongAccountCryptoVersion {
91 expected: String,
93 got: u32,
95 },
96 #[error("Crypto error, {0}")]
97 CryptoError(#[from] bitwarden_crypto::CryptoError),
98}
99
100#[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}