use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum BotError {
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("Telegram API error [{code}]: {description}")]
Api {
code: i64,
description: String,
retry_after: Option<i64>,
migrate_to_chat_id: Option<i64>,
},
#[error("Invalid bot token")]
InvalidToken,
#[error("{0}")]
Other(String),
}
impl BotError {
pub fn is_api_error_code(&self, expected_code: i64) -> bool {
matches!(self, BotError::Api { code, .. } if *code == expected_code)
}
pub fn flood_wait_seconds(&self) -> Option<i64> {
if let BotError::Api { retry_after, .. } = self {
return *retry_after;
}
None
}
}