use std::fmt::Debug;
use bitwarden_api_api::apis::Error as ApiApisError;
use bitwarden_api_identity::apis::Error as IdentityError;
use reqwest::StatusCode;
use thiserror::Error;
macro_rules! impl_bitwarden_error {
($name:ident, $error:ident) => {
impl<T> From<$name<T>> for $error {
fn from(e: $name<T>) -> Self {
match e {
$name::Reqwest(e) => Self::Reqwest(e),
$name::ResponseError(e) => Self::ResponseContent {
status: e.status,
message: e.content,
},
$name::Serde(e) => Self::Serde(e),
$name::Io(e) => Self::Io(e),
}
}
}
};
}
#[derive(Debug, Error)]
pub enum ApiError {
#[error(transparent)]
Reqwest(#[from] reqwest::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_bitwarden_error!(ApiApisError, ApiError);
impl_bitwarden_error!(IdentityError, ApiError);
#[derive(Debug, Error)]
#[error("The client is not authenticated or the session has expired")]
pub struct NotAuthenticatedError;
#[derive(Debug, Error)]
#[error("The response received was missing a required field: {0}")]
pub struct MissingFieldError(pub &'static str);
#[derive(Debug, Error)]
#[error("The client vault is locked and needs to be unlocked before use")]
pub struct VaultLockedError;
#[derive(Debug, thiserror::Error)]
#[error("Wrong password")]
pub struct WrongPasswordError;
#[derive(Debug, thiserror::Error)]
#[error("Missing private key")]
pub struct MissingPrivateKeyError;
#[macro_export]
macro_rules! require {
($val:expr) => {
match $val {
Some(val) => val,
None => return Err($crate::MissingFieldError(stringify!($val)).into()),
}
};
}