use std::fmt::Debug;
use bitwarden_api_base::Error as BaseApiError;
#[cfg(feature = "internal")]
use bitwarden_error::bitwarden_error;
use reqwest::StatusCode;
use thiserror::Error;
#[allow(missing_docs)]
#[derive(Debug, Error)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Error), uniffi(flat_error))]
pub enum ApiError {
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
#[error(transparent)]
ReqwestMiddleware(#[from] reqwest_middleware::Error),
#[error(transparent)]
Serde(#[from] serde_json::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Received error message from server: [{}] {}", .status, .message)]
ResponseContent { status: StatusCode, message: String },
}
impl<T> From<BaseApiError<T>> for ApiError {
fn from(e: BaseApiError<T>) -> Self {
match e {
BaseApiError::Reqwest(e) => Self::Reqwest(e),
BaseApiError::ReqwestMiddleware(e) => Self::ReqwestMiddleware(e),
BaseApiError::ResponseError(e) => Self::ResponseContent {
status: e.status,
message: e.content,
},
BaseApiError::Serde(e) => Self::Serde(e),
BaseApiError::Io(e) => Self::Io(e),
}
}
}
#[derive(Debug, Error)]
#[error("The client is not authenticated or the session has expired")]
pub struct NotAuthenticatedError;
#[derive(Debug, Error, serde::Serialize, serde::Deserialize, Clone)]
#[error("The client user ID is already set")]
pub struct UserIdAlreadySetError;
#[derive(Debug, Error)]
#[error("The response received was missing a required field: {0}")]
pub struct MissingFieldError(pub &'static str);
#[derive(Debug, thiserror::Error)]
#[error("Wrong password")]
pub struct WrongPasswordError;
#[derive(Debug, thiserror::Error)]
#[error("Missing private key")]
pub struct MissingPrivateKeyError;
#[cfg(feature = "internal")]
#[bitwarden_error(flat)]
#[derive(Debug, thiserror::Error)]
pub enum StatefulCryptoError {
#[error("Security state is required, but missing")]
MissingSecurityState,
#[error("Expected user in account cryptography version {expected}, but got {got}")]
WrongAccountCryptoVersion {
expected: String,
got: u32,
},
#[error("Crypto error, {0}")]
Crypto(#[from] bitwarden_crypto::CryptoError),
}
#[macro_export]
macro_rules! require {
($val:expr) => {
match $val {
Some(val) => val,
None => return Err($crate::MissingFieldError(stringify!($val)).into()),
}
};
}