use thiserror::Error;
#[derive(Error, Debug)]
pub enum FitbitError {
#[error("Request failed: {0}")]
RequestError(#[from] ureq::Error),
#[error("JSON parsing failed: {0}")]
JsonError(String),
#[error("Rate limit exceeded - retry after {0} seconds")]
RateLimitExceeded(u64),
#[error("Authentication failed: {0}")]
AuthenticationError(String),
#[error("API error: {status_code} - {message}")]
ApiError {
status_code: u16,
message: String,
},
#[error("Client configuration error: {0}")]
ConfigurationError(String),
#[error("Access token error: {0}")]
AccessTokenError(#[from] crate::access_token::AccessTokenError),
}
impl FitbitError {
pub fn api_error(status_code: u16, message: impl Into<String>) -> Self {
FitbitError::ApiError {
status_code,
message: message.into(),
}
}
pub fn authentication_error(message: impl Into<String>) -> Self {
FitbitError::AuthenticationError(message.into())
}
pub fn is_rate_limit(&self) -> bool {
matches!(self, FitbitError::RateLimitExceeded(_))
}
pub fn is_authentication_error(&self) -> bool {
matches!(self, FitbitError::AuthenticationError(_))
}
pub fn is_configuration_error(&self) -> bool {
matches!(self, FitbitError::ConfigurationError(_))
}
}