1use std::fmt::Debug;
4
5use bitwarden_api_base::{Error as BaseApiError, ResponseContent};
6#[cfg(feature = "internal")]
7use bitwarden_error::bitwarden_error;
8use thiserror::Error;
9
10#[allow(missing_docs)]
12#[derive(Debug, Error)]
13#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
14pub enum ApiError {
15 #[error(transparent)]
16 Reqwest(#[from] reqwest::Error),
17 #[error(transparent)]
18 ReqwestMiddleware(#[from] reqwest_middleware::Error),
19 #[error(transparent)]
20 Serde(#[from] serde_json::Error),
21 #[error(transparent)]
22 Io(#[from] std::io::Error),
23
24 #[error("Received error message from server: [{}] {}", _0.status, _0.message)]
25 Response(ResponseContent),
26}
27
28impl<T> From<BaseApiError<T>> for ApiError {
29 fn from(e: BaseApiError<T>) -> Self {
30 match e {
31 BaseApiError::Reqwest(e) => Self::Reqwest(e),
32 BaseApiError::ReqwestMiddleware(e) => Self::ReqwestMiddleware(e),
33 BaseApiError::Response(e) => Self::Response(e),
34 BaseApiError::Serde(e) => Self::Serde(e),
35 BaseApiError::Io(e) => Self::Io(e),
36 BaseApiError::_Phantom(_, _) => unreachable!(),
37 }
38 }
39}
40
41impl From<ResponseContent> for ApiError {
42 fn from(value: ResponseContent) -> Self {
43 Self::Response(value)
44 }
45}
46
47#[derive(Debug, Error)]
49#[error("The client is not authenticated or the session has expired")]
50pub struct NotAuthenticatedError;
51
52#[derive(Debug, Error, serde::Serialize, serde::Deserialize, Clone)]
54#[error("The client user ID is already set")]
55pub struct UserIdAlreadySetError;
56
57#[derive(Debug, Error)]
59#[error("The response received was missing a required field: {0}")]
60pub struct MissingFieldError(pub &'static str);
61
62#[derive(Debug, thiserror::Error)]
64#[error("Wrong password")]
65pub struct WrongPasswordError;
66
67#[derive(Debug, thiserror::Error)]
69#[error("Missing private key")]
70pub struct MissingPrivateKeyError;
71
72#[cfg(feature = "internal")]
75#[bitwarden_error(flat)]
76#[derive(Debug, thiserror::Error)]
77pub enum StatefulCryptoError {
78 #[error("Security state is required, but missing")]
81 MissingSecurityState,
82 #[error("Expected user in account cryptography version {expected}, but got {got}")]
84 WrongAccountCryptoVersion {
85 expected: String,
87 got: u32,
89 },
90 #[error("Crypto error, {0}")]
91 Crypto(#[from] bitwarden_crypto::CryptoError),
92}
93
94#[macro_export]
99macro_rules! require {
100 ($val:expr) => {
101 match $val {
102 Some(val) => val,
103 None => return Err($crate::MissingFieldError(stringify!($val)).into()),
104 }
105 };
106}