use crate::api::ResponseCode;
pub type Result<T> = std::result::Result<T, ProtonError>;
#[derive(Debug, thiserror::Error)]
pub enum ProtonError {
#[error(transparent)]
Api(#[from] ProtonApiError),
#[error("HTTP transport error: {0}")]
Transport(#[from] reqwest::Error),
#[error("serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("cryptography error: {0}")]
Crypto(#[from] crate::crypto::CryptoError),
#[error("invalid operation: {0}")]
InvalidOperation(String),
}
impl ProtonError {
pub fn invalid_operation(message: impl Into<String>) -> Self {
Self::InvalidOperation(message.into())
}
}
#[derive(Debug, Clone, thiserror::Error)]
#[error("proton api error {code:?} (http {http_status}): {message}")]
pub struct ProtonApiError {
pub code: ResponseCode,
pub http_status: u16,
pub message: String,
}
impl ProtonApiError {
pub fn is_unauthorized(&self) -> bool {
self.http_status == 401
}
pub fn is_invalid_refresh_token(&self) -> bool {
matches!(self.code, ResponseCode::InvalidRefreshToken)
}
pub fn is_insufficient_scope(&self) -> bool {
matches!(self.code, ResponseCode::InsufficientScope)
}
}